How to Deploy GitLab Server Using Docker and Ubuntu Server 22.04 – The New Stack

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 many advantages (such as availability and reliability), there is something to be said for having full control over your repositories so that no one can access it without your approval.

With the help of Ubuntu Server 22.04 and Docker, you can do that. And I’ll show you how it’s done. It’s not too complicated, but there are a number of steps required. And so, without further ado, let’s get to work.

To accomplish this task, you will need a running instance of Ubuntu Server 22.04 and a user with sudo privileges. The Ubuntu instance can be hosted on your LAN, or even in your cloud-hosted account (although hosting it through a third-party type defeats the purpose of a self-hosted repository). Either way, you’re ready to do some magic.

Install dependencies

The first thing we are going to do is install the required dependencies. Connect 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 will 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.

Change default SSH port

Since GitLab uses the default SSH port, you need to change the default SSH server port. Otherwise, there will be a conflict. Open the SSH configuration file with:

sudo nano /etc/ssh/sshd_config

In this file, find the line:

Replace this line with:

Enable port 22 to pass through the firewall with:

sudo ufw allow 2022

Be sure to test the SSH connection with another login using the command:

ssh USER@SERVER -p 2022

Where USER is your remote username and SERVER is the IP address or domain of the hosting server.

Create a new Docker volume

We are now ready to move on to the Docker side of things. The first thing we are going to do is create a new volume. First, create a directory to host the files with:

sudo mkdir -p /srv/gitlab

Next, create a directory that will host our Docker compose file with:

mkdir ~/docker-gitlab

Access this directory with:

cd ~/docker-gitlab

Create a file to house the environment variables with:

nano .env

Paste the following into this new file:

GITLAB_HOME=/srv/gitlab

Save and close the file.

Create the Docker Compose file

Create a new composition file with:

nano docker-compose.yml

In that file, paste the following (be sure to change anything in bold depending on your environment/needs):

Save and close the file.

Deploy container

We are now ready to deploy the container. To do this, issue the command:

docker-compose up -d

Deploying the container will take some time (between 10 and 30 minutes, depending on the speed of your network connection), so sit back and watch the output go by or take care of another task. Once the deployment is complete, you will 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 act as the root login password.

Access to GitLab

Open a web browser and point it to http://SERVER (where SERVER is your server’s IP address or domain). You will be greeted by the GitLab login screen (see Figure 1), where you will type the username root and paste the password you found in the initial_root_password file, as shown above. If the site doesn’t appear immediately, give it some time for the containers to finish deploying. Keep refreshing your web browser until the login screen appears.

Figure 1: The GitLab login screen is synonymous with success!

An alternative method of deployment

If you are having trouble with the above deployment, here is another method.

Configure the volume location with:

Deploy the container with this (be sure to change everything in bold to suit your needs):

One of the above methods should work to deploy GitLab. If you’re still having trouble, you can change the outward-facing SSH port to something like 10022, so this option would look like —post 10022:22.

Finally, if you’re still having trouble deploying GitLab, 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 in your local network.

The New Stack is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Enable, Docker.

Comments are closed.