How to Setup GitLab using Docker

Having your own Git repository server can be really helpful. If like me you get a bit paranoid about people seeing your messy code, or you want code that you fully own. That’s where using GitLab can help, especially using it with Docker. It’s a Git server solution with all the bells and whistles, at an extremely inviting price point. Free. Granted that your using the community edition.

GitLab can be installed directly onto your server. There are many guides on how to do this on the GitLab website. However, using a solution like Docker can open your server up to being able to host multiple applications without the fear of them conflicting.

This guide will focus mainly on an Ubuntu server, however, as Docker is a cross platform system. This guide should work on any operating system. If you haven’t already installed Docker on Ubuntu, check out my guide found here.

Directory Setup

When using GitLab with Docker I highly recommend using Docker Compose. With this you can can make changes to the settings quickly and easier than just using the command line. Before GitLab can be started the directory needs to be setup first.

There are only three sub-directories that are needed. We need to create a directory for config files, one for log files, and another to store data.

This is now the directory setup and ready to go. The next thing we need to create is the docker compose file.

GitLab Docker Compose File

According to the Docker documentation.

Compose is a tool for defining and running multi-container Docker applications.

However we will be using it to setup volumes, and create custom GitLab configuration options.

The first thing we do is create a docker-compose.yml file in the GitLab directory. We can then setup the file with what image we will be using. To find the latest version of GitLab on Docker we can either use the latest tag. Or by going to Docker hub we can pick a specific version. For this guide I will be using 12.1.6-ce.0.

git:
  image: 'gitlab/gitlab-ce:12.1.6-ce.0'

We can then start to add some of the really important parts like the ports, and the volumes.

ports:
  - '80:80'
  - '443:443'
  - '22:22'
Volumes:
  - './Config:/etc/gitlab'
  - './Logs:/var/log/gitlab'
  - './Data:/var/opt/gitlab'

With this we can start using GitLab. If we run docker-compose up from the GitLab directory this will start GitLab. We can then access it from any web browser using the ip address. However, if we shutdown the computer GitLab wont start back up again. This can be sorted with the restart option.

restart: always

We know have a GitLab server that has the right ports, volumes and restarts when the computer does. Now we can get to the fun part. The custom GitLab configuration.

Any settings in the gitlab.rb file can be added to this file. I have added some examples below.

environment:
    GITLAB_OMNIBUS_CONFIG: |
      gitlab_rails['smtp_enable'] = true
      gitlab_rails['smtp_address'] = "smtp.gmail.com"
      gitlab_rails['smtp_port'] = 587
      gitlab_rails['smtp_authentication'] = "login"
      gitlab_rails['smtp_enable_starttls_auto'] = true
      gitlab_rails['smtp_tls'] = false
      gitlab_rails['smtp_openssl_verify_mode'] = 'peer'
      gitlab_rails['smtp_user_name'] = "user@gmail.com"
      gitlab_rails['smtp_password'] = "reallysecurepassword"
      gitlab_rails['smtp_domain'] = "smtp.gmail.com"

In the above example I have added the Gmail SMTP settings. There are other configurations for SMTP settings that can be found as normal on the GitLab documentation.

Full Docker Compose File

Here is the full file that we have created, just incase you want to just copy and paste the whole thing.

git:
  image: 'gitlab/gitlab-ce:12.1.6-ce.0'
  user: 'root'
  restart: always
  hostname: 'gitlab.jrlbyrne.com'
  environment:
    GITLAB_OMNIBUS_CONFIG: |
      gitlab_rails['smtp_enable'] = true
      gitlab_rails['smtp_address'] = "smtp.gmail.com"
      gitlab_rails['smtp_port'] = 587
      gitlab_rails['smtp_authentication'] = "login"
      gitlab_rails['smtp_enable_starttls_auto'] = true
      gitlab_rails['smtp_tls'] = false
      gitlab_rails['smtp_openssl_verify_mode'] = 'peer'
      gitlab_rails['smtp_user_name'] = "user@gmail.com"
      gitlab_rails['smtp_password'] = "reallysecurepassword"
      gitlab_rails['smtp_domain'] = "smtp.gmail.com"
  ports:
  - '80:80'
  - '443:443'
  - '22:22'
Volumes:
  - './Config:/etc/gitlab'
  - './Logs:/var/log/gitlab'
  - './Data:/var/opt/gitlab'

Leave a Reply