GitLab docker on Ubuntu

Today I decided to move my self-hosted gitlab instance to Docker so that I will not need to reinstall and configure lots of things when migrating from one host to another host. So I blog this entry to note steps to install gitlab docker on Ubuntu.

Install Docker CE (or Docker Compose if you want)

  1. Simply install Docker-CE:
  2. OR we can download and use docker-compose:


Run GitLab with Docker-CE

  1. Init and run the gitlab docker image:
  2. Enter the docker container (with docker exec -ti gitlab bash) to see if configuration is correct to re-run gitlab-ctl reconfigure. You will need to check the external_url variable in /etc/gitlab/gitlab.rb.
  3. Backup your current gitlab, and then transfer to current host server. Remember that in the previous step, we set /var/opt/gitlab in the gitlab container mounted to /srv/gitlab/data in the host machine. So we copy the backup file to /srv/gitlab/data/backups/ and restore the backup:
  4. Do final check on your new gitlab before pointing your DNS to the new IP.
  5. Setup backup with cron on the host machine: 0 0 * * * docker exec -t gitlab gitlab-rake gitlab:backup:create
  6. Setup Let’s Encrypt: we will request certs from host machine and change the gitlab.rb in gitlab container to point to the certs.
    1. Install certbot:
    2. Request for certifications from host machine (we will need to stop docker container since it binds to the port 80):
    3. With the above commands, certs will be stored at /etc/letsencrypt/live/ of the host machine. We will copy certs to gitlab data folder to use it inside gitlab container:
    4. Change /srv/gitlab/config/gitlab.rb to reflect these certs. Add the following nginx lines and change external_url inside it:
    5. Reconfigure gitlab:
    6. Setup cron to renew certs on monthly basis (create a /root/cron-gitlab-certbot-renewal):

Upgrade GitLab to latest version

  1. Remove container, pull the latest image and run it again:

 Backup Docker container to store remotely

  1. First, get the container ID with docker ps command.
  2. To backup a container CONTAINER_ID, user docker commit as follows (we can use either CONTAINER_ID or CONTAINER_NAME. I use name as gitlab instead):

    After this, the backup is saved as a docker image. We can view with docker images.
  3. Save backup as tar file to rsync to remote backup storage:

Restore Docker container from saved backup

  1. Load the backup saved image:
  2. Start a new container with the loaded image:

    Note that in this case, the base image is gitlab_backup, not gitlab/gitlab-ce:latest. We can simply rsync the /srv/gitlab folder to the new server and then run with the gitlab-ce:latest image.


With Docker Stack

Basically, we will need to separate GitLab services (Redis, Postgres, Prometheus) into separated ones. So I will note configurations as well as some steps as follows:

  1. Some configuration files:
    • Init docker swarm and Create a new balancer network
    • docker-compose.yml“:
    • swarm-configs/gitlab.rb“:
    • swarm-configs/prometheus.yaml“:
    • swarm-configs/grafana.ini“:
  2. Then, I start the docker stack:
  3. Then, I prepare backup data for restoration. This simply copies the Gitlab backup one to the “/srv/gitlab/data/backups/” folder.
  4. Then, I need to stop the “unicorn” and “sidekiq” in the “gitlab” container before continuing on backup restoration. Then do restoration:
  5. Finally, restart the Gitlab service:

    And set up a cron to backup the gitlab container daily
  6. IF you want to run multiple instances for the Gitlab service, this is the time to change the deployment parameters.

Leave a Reply