How to Self‑Host GitLab on Ubuntu with Docker: Step‑by‑Step Guide
Learn how to securely self‑host a GitLab instance on an Ubuntu server using Docker, covering dependency installation, SSH port configuration, Docker volume setup, compose file creation, container deployment, and accessing the web interface, with alternative deployment methods for troubleshooting.
Install Dependencies
First install required packages on the Ubuntu instance:
sudo apt install ca-certificates curl openssh-server apt-transport-https gnupg lsb-release -yThen add Docker’s official GPG key and repository, update apt, and install Docker CE, CLI, containerd, and docker‑compose:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose -yAdd your user to the docker group and re‑login:
sudo usermod -aG docker $USERChange Default SSH Port
Modify the SSH daemon configuration to avoid conflict with GitLab’s default port: sudo vi /etc/ssh/sshd_config Replace the line #prot 22 with: prot 2222 Allow the new port through the firewall and test the connection:
sudo ufw allow 2022 ssh USER@SERVER -p 2022Create a Docker Volume
Create directories for GitLab data and the Docker‑compose files:
sudo mkdir -p /srv/gitlab mkdir ~/docker-gitlab cd ~/docker-gitlabPrepare Environment File
Create an .env file with the GitLab home path:
vi .env GITLAB_HOME=/srv/gitlabCreate Docker‑Compose File
Create compose.yml and paste the following configuration (adjust bold placeholders as needed):
version:'3.6'
services:
web:
image:'gitlab/gitlab-ee:latest'
container_name:'gitlab-server'
restart:always
hostname:'gitlab.example.com'
environment:
GITLAB_OMNIBUS_CONFIG:|
external_url 'https://DOMAIN_OR_IP'
gitlab_rails['smtp_enable']=true
gitlab_rails['smtp_address']="SMTP_SERVER"
gitlab_rails['smtp_user_name']="SMTP_SERVER_USERNAME"
gitlab_rails['smtp_password']="SMTP_SERVER_PASSWORD"
gitlab_rails['smtp_domain']="DOMAIN"
gitlab_rails['smtp_enable_starttls_auto']=true
gitlab_rails['smtp_port']=587
gitlab_rails['smtp_authentication']="login"
gitlab_rails['gitlab_email_from']='FROM_EMAIL'
gitlab_rails['gitlab_email_reply_to']='REPLY_EMAIL'
# Add any other gitlab.rb configuration here, each on its own line
ports:
- '80:80'
- '443:443'
- '22:22'
- '587:587'
volumes:
- '$GITLAB_HOME/config:/etc/gitlab'
- '$GITLAB_HOME/logs:/var/log/gitlab'
- '$GITLAB_HOME/data:/var/opt/gitlab'
shm_size:'256m'Deploy the Container
Start the services in detached mode: docker-compose up -d After a few minutes, retrieve the automatically generated root password:
sudo cat /srv/gitlab/config/initial_root_passwordAccess GitLab
Open a web browser and navigate to http://SERVER (replace SERVER with your IP or domain). Log in as user root using the password obtained above.
Alternative Deployment Methods
If the compose method fails, you can set the volume location manually: export GITLAB_HOME=/srv/gitlab Then run GitLab directly with Docker:
docker run --detach \
--hostname gitlab.example.com \
--publish 443:443 --publish 80:80 --publish 22:22 \
--name gitlab \
--restart always \
--volume $GITLAB_HOME/config:/etc/gitlab \
--volume $GITLAB_HOME/logs:/var/log/gitlab \
--volume $GITLAB_HOME/data:/var/opt/gitlab \
--shm-size 256m \
gitlab/gitlab-ee:latestOr use the community edition image:
docker run -d -p 22:22 -p 80:80 -p 443:443 \
--name gitlab --hostname gitlab.example.com \
--restart unless-stopped --shm-size 256m \
-v gitlab_config:/etc/gitlab -v gitlab_logs:/var/log/gitlab \
-v gitlab_data:/var/opt/gitlab \
gitlab/gitlab-ce:14.7.0-ce.0These steps give you a fully functional, internally hosted GitLab repository.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
