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:
    apt-get update -y
    apt-get install -y apt-transport-https ca-certificates curl software-properties-common
    curl -fsSL | apt-key add -
    add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable"
    apt-get update -y
    apt-get install -y docker-ce
    # You can also list version to install exact version with "apt-cache madison docker-ce" and "apt-get install docker-ce=<VERSION>"
  2. OR we can download and use docker-compose:
    curl -L`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
    chmod +x /usr/local/bin/docker-compose
    docker-compose --version


Run GitLab with Docker-CE

  1. Init and run the gitlab docker image:
    # I add a new IP address ADDITIONAL_IP_ADDRESS to docker host to run later
    # If you do not want to bind with static IP, just remove the ADDITIONAL_IP_ADDRESS part
    ip address add ADDITIONAL_IP_ADDRESS dev eno1
    docker run --detach \
        --hostname \
        --env GITLAB_OMNIBUS_CONFIG="external_url ''; gitlab_rails['lfs_enabled'] = true;" \
        --publish ADDITIONAL_IP_ADDRESS:443:443 --publish ADDITIONAL_IP_ADDRESS:80:80 --publish ADDITIONAL_IP_ADDRESS: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 \
  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:
    mv /root/1517992991_2018_02_07_10.4.2_gitlab_backup.tar /srv/gitlab/data/backups/
    docker exec -t gitlab gitlab-ctl stop unicorn
    docker exec -t gitlab gitlab-ctl stop sidekiq
    docker exec -t gitlab chown -R git:git /var/opt/gitlab/backups
    docker exec -it gitlab gitlab-rake gitlab:backup:restore BACKUP=1517992991_2018_02_07_10.4.2
    # Restart gitlab and check
    docker exec -it gitlab gitlab-ctl start
    docker exec -it gitlab gitlab-rake gitlab:check SANITIZE=true
  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:
      add-apt-repository ppa:certbot/certbot
      apt-get update -y
      apt install -y certbot
    2. Request for certifications from host machine (we will need to stop docker container since it binds to the port 80):
      docker stop gitlab
      certbot certonly --standalone -d -d -d
      docker start gitlab
    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:
      mkdir /srv/gitlab/data/certbot
      cp /etc/letsencrypt/live/ /srv/gitlab/data/certbot/fullchain.pem
      cp /etc/letsencrypt/live/ /srv/gitlab/data/certbot/privkey.pem
    4. Change /srv/gitlab/config/gitlab.rb to reflect these certs. Add the following nginx lines and change external_url inside it:
      ### Add the following lines to GitLab NGINX section
      nginx['ssl_certificate'] = "/var/opt/gitlab/certbot/fullchain.pem"
      nginx['ssl_certificate_key'] = "/var/opt/gitlab/certbot/privkey.pem"
      ### Change this line
      external_url ''
    5. Reconfigure gitlab:
      docker exec -it gitlab gitlab-ctl reconfigure
    6. Setup cron to renew certs on monthly basis (create a /root/cron-gitlab-certbot-renewal):
      # Create a cron with 0 0 1 * * bash  /root/
      docker stop gitlab
      certbot renew
      cp /etc/letsencrypt/live/ /srv/gitlab/data/certbot/fullchain.pem
      cp /etc/letsencrypt/live/ /srv/gitlab/data/certbot/privkey.pem
      docker start gitlab

Upgrade GitLab to latest version

  1. Remove container, pull the latest image and run it again:
    docker stop gitlab
    docker rm gitlab
    docker pull gitlab/gitlab-ce:latest
    docker run --detach \
    --hostname \
    --publish ADDITIONAL_IP_ADDRESS:443:443 --publish ADDITIONAL_IP_ADDRESS:80:80 --publish ADDITIONAL_IP_ADDRESS: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 \

 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):
    docker commit -p gitlab gitlab_backup

    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:
    docker save -o /srv/gitlab/gitlab_container_backup.tar gitlab_backup

Restore Docker container from saved backup

  1. Load the backup saved image:
    docker load < /srv/gitlab/gitlab_container_backup.tar
  2. Start a new container with the loaded image:
    docker run --detach \
        --hostname \
        --env GITLAB_OMNIBUS_CONFIG="external_url ''; gitlab_rails['lfs_enabled'] = true;" \
        --publish ADDITIONAL_IP_ADDRESS:443:443 --publish ADDITIONAL_IP_ADDRESS:80:80 --publish ADDITIONAL_IP_ADDRESS: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 \

    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 swarm init
      docker network create -d overlay balancer
    • docker-compose.yml“:
      version: "3.4"
          image: "postgres:9.6" # The embedded postgres of gitlab till 11.x is 9.6
            - "/srv/gitlab-swarm/postgres:/data"
            POSTGRES_USER: "gitlab"
            POSTGRES_PASSWORD: "gitlab"
            PGDATA: "/data"
            POSTGRES_DB: "gitlab"
          image: "gitlab/gitlab-ce:latest"
            - "/srv/gitlab-swarm/gitlab/data:/var/opt/gitlab"
            - "/srv/gitlab-swarm/gitlab/logs:/var/log/gitlab"
            - "/srv/gitlab-swarm/gitlab/config:/etc/gitlab"
      #      - "/srv/gitlab/data:/var/opt/gitlab"
      #      - "/srv/gitlab/logs:/var/log/gitlab"
      #      - "/srv/gitlab/config:/etc/gitlab"
            - "2222:22"
            - "8888:80"
            - source: "gitlab.rb"
              target: "/etc/gitlab/gitlab.rb"
            - postgres
            - default
            - balancer
            replicas: 1
              condition: on-failure
              - "traefik.port=80"
              - "traefik.backend=gitlab"
              - ""
              - ","
          image: "redis"
          image: "prom/prometheus"
          command: "--config.file=/prometheus.yaml --storage.tsdb.path /data"
            - "/srv/gitlab-swarm/prometheus:/data"
            - prometheus.yaml
            - default
            - balancer
              traefik.port: 9090
              traefik.frontend.rule: ""
          image: grafana/grafana
            GF_PATHS_CONFIG: "/grafana.ini"
            - grafana.ini
            - "/srv/gitlab-swarm/grafana:/data"
            - default
            - balancer
              traefik.port: 3000
              traefik.frontend.rule: ""
          file: "./swarm-configs/gitlab.rb"
          file: "./swarm-configs/prometheus.yaml"
          file: "./swarm-configs/grafana.ini"
            name: balancer
    • swarm-configs/gitlab.rb“:
      external_url ''
      #registry_external_url ''
      # Disable services
      postgresql['enable'] = false
      redis['enable'] = false
      prometheus['enable'] = false
      postgres_exporter['enable'] = false
      redis_exporter['enable'] = false
      # SMTP 
      gitlab_rails['smtp_enable'] = true
      gitlab_rails['smtp_address'] = ""
      gitlab_rails['smtp_port'] = 465
      gitlab_rails['smtp_user_name'] = "XXXXXXXXXXXXX"
      gitlab_rails['smtp_password'] = "XXXXXXXXXXXXX"
      gitlab_rails['smtp_domain'] = ""
      gitlab_rails['smtp_authentication'] = "login"
      gitlab_rails['smtp_enable_starttls_auto'] = true
      gitlab_rails['smtp_tls'] = true
      # Postgres settings
      gitlab_rails['db_adapter'] = "postgresql"
      gitlab_rails['db_encoding'] = "unicode"
      # database service will be named "postgres" in the stack
      gitlab_rails['db_host'] = "postgres" 
      gitlab_rails['db_database'] = "gitlab"
      gitlab_rails['db_username'] = "gitlab"
      gitlab_rails['db_password'] = "gitlab"
      # Redis settings
      # redis service will be named "redis" in the stack
      gitlab_rails['redis_host'] = "redis"
      # Prometheus exporters
      node_exporter['listen_address'] = ''
      gitlab_monitor['listen_address'] = ''
      gitaly['prometheus_listen_addr'] = ""
      gitlab_workhorse['prometheus_listen_addr'] = ""
    • swarm-configs/prometheus.yaml“:
        scrape_interval: 15s
        - job_name: 'prometheus'
            - targets: ['localhost:9090']
        # gitlab monitor
        - job_name: 'gitlab_monitor'
            - targets: ['gitlab:9168']
        # gitlab sidekiq
        - job_name: 'gitlab_sidekiq'
          metrics_path: /sidekiq
            - targets: ['gitlab:9168']
        # gitlab process
        - job_name: 'gitlab_process'
          metrics_path: /process
            - targets: ['gitlab:9168']
        # gitlab pages
        - job_name: 'gitlab_pages'
            - targets: ['gitlab:9235']
        # gitaly
        - job_name: gitaly
            - targets: ['gitlab:9236']
        # gitlab workhorse
        - job_name: workhorse
            - targets: ['gitlab:9229']
    • swarm-configs/grafana.ini“:
      path = "/data/grafana.db"
      provider = "redis"
      provider_config = "addr=redis:6379,prefix=grafana:"
  2. Then, I start the docker stack:
    docker stack deploy -c docker-compose.yml gitlab
  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:
    docker exec -t __CONTAINER_NAME__ gitlab-ctl stop unicorn
    docker exec -t __CONTAINER_NAME__ gitlab-ctl stop sidekiq
    docker exec -it __CONTAINER_NAME__  gitlab-rake gitlab:backup:restore BACKUP=XXXX_BACKUP_TIMESTAMP_XXXX
  5. Finally, restart the Gitlab service:
    docker exec -t __CONTAINER_NAME__ gitlab-ctl restart

    And set up a cron to backup the gitlab container daily

    0 0 * * * docker exec -it $(docker ps | grep gitlab_gitlab | cut -d " " -f 1) gitlab-rake gitlab:backup:create
  6. IF you want to run multiple instances for the Gitlab service, this is the time to change the deployment parameters.

About NhocConan

A super lazy guy who tries to write tech blog entries in English.He is lazy, so he can only write when he is in a good mood or when he is tired of coding.

Leave a comment

Your email address will not be published. Required fields are marked *