TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Containers / Software Development

How to Deploy GitLab Server Using Docker and Ubuntu

Have you ever wanted to host your own GitLab repositories? With the help of both Ubuntu Server and Docker, you can do just that.
Feb 26th, 2024 10:15am by
Featued image for: How to Deploy GitLab Server Using Docker and Ubuntu
Have you ever wanted to host your own GitLab repositories to ensure your code never falls into the wrong hands? Although hosting your repositories on a third-party cloud host has plenty of advantages (such as availability and reliability), there’s something to be said about having total control over your repositories so that no one can access it without your approval. With the help of both Ubuntu Server and Docker, you can do just that. And I’m going to show you how it’s done. It’s not overly complicated, but there are a number of steps required. And so, without further ado, let’s get to work. To accomplish this task, you’ll need a running instance of Ubuntu Server and a user with sudo privileges. The instance of Ubuntu can be hosted on your LAN, or even in your cloud-hosted account (although hosting it via a third-party kind of defeats the purpose of a self-hosted repository). Either way, you’re ready to make some magic.

Install the Dependencies

The first thing we’ll do is install the required dependencies. Log in to your Ubuntu instance and install the required software with the command:
sudo apt install ca-certificates curl openssh-server apt-transport-https gnupg lsb-release -y
Next, we need to install the Community Edition of Docker. For this, we’ll add the official Docker GPG key with:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Next, add the Docker repository:
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
Update apt with the command:
sudo apt-get update
Finally, install Docker Community Edition with:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose -y
Add your user to the docker group with:
sudo usermod -aG docker $USER
Log out and log back in for the changes to take effect. So far, so good. Let’s move on.

Run GitLab on a Single Line

You can opt to deploy the GitLab server with a single-line docker command like this:
docker run --detach \

--hostname HOSTNAME \

--env GITLAB_OMNIBUS_CONFIG="external_url 'http://SERVER'; gitlab_rails['lfs_enabled'] = true;" \

--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:VERSION
Where HOSTNAME is the hostname of the server (and must be externally reachable), SERVER is either the IP address or domain of the hosting server, and VERSION is the version number (such as 16.5.3 or latest).\ Next, the Docker Compose file needs to be changed (as some of it no longer functions properly). That new code looks like this:
version: '3.6'

services:

  gitlab:

    image: gitlab/gitlab-ee:VERSION

    container_name: gitlab

    restart: always

    hostname: 'HOSTNAME'

    environment:

      GITLAB_OMNIBUS_CONFIG: |

        # Add any other gitlab.rb configuration here, each on its own line

        external_url 'https://SERVER'

    ports:

      - '80:80'

      - '443:443'

      - '22:22'

    volumes:

      - '$GITLAB_HOME/config:/etc/gitlab'

      - '$GITLAB_HOME/logs:/var/log/gitlab'

      - '$GITLAB_HOME/data:/var/opt/gitlab'

    shm_size: '256m'
Where HOSTNAME is the hostname of the server (and must be externally reachable), SERVER is either the IP address or domain of the hosting server, and VERSION is the version number (such as 16.5.3 or latest).

Change the Default SSH Port

Because GitLab uses the default SSH port, you must change the default SSH server port. Otherwise, there’ll be a conflict. Open the SSH config file with:
sudo nano /etc/ssh/sshd_config
In that file, look for the line:
#Port 22
Change that line to:
Port 2022
Enable port 22 to pass through the firewall with: sudo ufw allow 2022 With the firewall opened, restart SSH with the command:
sudo systemtl restart ssh
Make sure to test the SSH connection with another login using the command:
ssh USER@SERVER -p 2022
Where USER is your remote user name and SERVER is the IP address or domain of the hosting server

Create a New Docker Volume

We’re now ready to move on to the Docker side of things. The first thing we’ll do is create a new volume. First, create a directory to house the files with:
sudo mkdir -p /srv/gitlab
Next, create a directory that will house our Docker compose file with:
mkdir ~/docker-gitlab
Change into that directory with:
cd ~/docker-gitlab
Create a file to house environment variables with:
nano .env
Paste the following into that new file:
GITLAB_HOME=/srv/gitlab
Save and close the file.

Create the Docker Compose File

Create a new compose file with:
nano docker-compose.yml
In that file, paste the following (make sure to change anything in bold to suit your environment/needs):
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'
Save and close the file.

Deploy the Container

We’re now ready to deploy the container. To do that, issue the command: docker-compose up -d The deployment of the container will take some time (anywhere between 10-30 minutes, depending on the speed of your network connection), so either sit back and watch the output fly by or take care of some other task. When the deployment completes, you’ll need to access the automatically generated root password with the command:
sudo cat /srv/gitlab/config/initial_root_password
You should see a long string of random characters that will serve as your root password login.

Accessing GitLab

Open a web browser and point it to http://SERVER (where SERVER is the IP address or domain of your server). You’ll be greeted by the GitLab login screen (see Figure 1), where you’ll type the username root and paste the password you found in the intial_root_password file, as shown above. If the site doesn’t come up immediately, give it some time for the containers to finish being deployed. Keep refreshing your web browser until the login screen appears.
Figure 1: The GitLab login screen means success!

An Alternative Method of Deployment

If you find you have trouble with the above deployment, here’s another method. Set up the volume location with:
export GITLAB_HOME=/srv/gitlab
Deploy the container with this (make sure to change anything in bold to suit your needs):
docker run --detach \
  --hostname HOSTNAME \
  --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:latest
One of the above methods should work to get GitLab deployed. If you still have problems, you may change the outward-facing SSH port to something like 10022, so that option would look like —publish 10022:22. Finally, if you still have problems getting GitLab to deploy, here’s another option:
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.0
Congratulations! You now have a working GitLab repository that can be used within your LAN. (This post, which has been updated, originally appeared in July 2022)
Group Created with Sketch.
TNS owner Insight Partners is an investor in: Docker, Enable.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.