Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Deploy GitLab Through Docker

Deploying Gitlab through docker has become a lot simpler than it used to be.

Related Posts

Requirements

  • At least 2GB RAM, preferably 3+

Steps

If you haven't already, install docker, then install docker compose.

Next, we need to change the server's SSH port from the default 22 to something else such as 2222 or 23825. You can do this by editing the /etc/ssh/sshd_config file and changing the line below

...
# What ports, IPs and protocols we listen for 
Port 22
...

You will need to remember this so that next time you SSH into the server, you specify the port with -p like so:

ssh -p 2222 my-host-or-ip.org

Now we can deploy the gitlab container by executing the following script:

HOSTNAME="gitlab.mydomain.com"

sudo docker run \
  --detach \
  --hostname $HOSTNAME \
  --publish 443:443 \
  --publish 80:80 \
  --publish 22:22 \
  --name gitlab \
  --restart always \
  --volume $PWD/gitlab/config:/etc/gitlab \
  --volume $PWD/gitlab/logs:/var/log/gitlab \
  --volume $PWD/gitlab/data:/var/opt/gitlab \
  --log-driver json-file \
  --log-opt max-size=10m \
  --log-opt max-file=3 \
  gitlab/gitlab-ce:latest

Don't forget to set the HOSTNAME. The hostname must not start with http:// or https://. I would recommend setting up a secondary "data" disk through LVM, and performing this action/script within there. This will save you a lot of effort later if your server starts to run out of space and you need to expand. Moving the volume files correctly without messing up GItlab is almost impossible, whereas with LVM, you could add a disk, and expand across that. This will allow you to retain all your existing KVM snapshots, which expanding a qcow2 disk image does not allow you to do.

After the container has deployed, you will need to wait a while before the website will appear when you type your server's IP or hostname into your browser's URL.

Default User

To log in, the default user is root. You will then need to find out what the password is, which can be found by executing the following command on the host:

sudo docker exec -it \
  gitlab \
  grep 'Password:' /etc/gitlab/initial_root_password

Updating Gitlab

Run the following commands to update to the latest version of Gitlab. However, I recommend taking a snapshot of your server first just in case.

# Pull the latest image
docker pull gitlab/gitlab-ce:latest

# kill and remove the existing container
docker stop `gitlab`
docker rm `gitlab`

# Launch a new container using the new image.
sudo docker run \
  --detach \
  --hostname gitlab.example.com \
  --publish 443:443 \
  --publish 80:80 \
  --publish 22:22 \
  --name gitlab \
  --restart always \
  --volume /srv/gitlab/config:/etc/gitlab \
  --volume /srv/gitlab/logs:/var/log/gitlab \
  --volume /srv/gitlab/data:/var/opt/gitlab \
  --log-driver json-file \
  --log-opt max-size=10m \
  --log-opt max-file=3 \
  gitlab/gitlab-ce:latest

This will always pull the latest image, irrespective of major releases. You may need to manually specify image versions when there has been a major release change, in order to ensure that you upgrade to the latest version of the previous major release, before upgrading to the next major release version. Refer to the upgrade paths.

References

Last updated: 11th August 2022
First published: 16th August 2018