Menu Close

Gitlab with Docker and Dokku

Gitlab is a fantastic git repository manager, it’s essentially github but with no limit on private repositories and most important, it’s absolutely open source.

You know, configure a new platform or service can often be a pain, but the good thing of docker is that with a single command we can have any service online in few seconds!

Step 1: Start Gitlab

First of all, we need to start the docker container of gitlab:

sudo docker run --detach \
 --hostname git.yourdomain.com \
 --publish 8080:80 --publish 2222:22 \
 --name gitlab \
 --restart always \
 --volume /home/volumes/gitlab/config:/etc/gitlab \
 --volume /home/volumes/gitlab/logs:/var/log/gitlab \
 --volume /home/volumes/gitlab/data:/var/opt/gitlab \
 gitlab/gitlab-ce:latest

Keep attention on parameters:

  • hostname” should be your domain name where git will be available
  • name” is the container name
  • restart always” tell to docker that the container have to be always running after a restart of the system
  • volume” configures persistent storages for important files of gitlab
  • gitlab/gitlab-ce:latest” is the preconfigured docker container (community edition)

Gitlab site is now available at git.yourdomain.com:8080, see step 3 for set to a different port in a dokku environment.

We have also set ssh port to 2222, so we need to change gitlab standard ssh port in /etc/gitlab/gitlab.rb:

gitlab_rails['gitlab_shell_ssh_port'] = 2222

Remember to run gitlab-ctl reconfigure after save it.

Step 2: Configure Emails

Gitlab will sends lots of mail for notify users about activity on repos, so you should config smtp settings.
Here you can find generic settings for most common services: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/smtp.md
If you have, as me, a private postfix installation, you can use it with the follow settings:


 gitlab_rails['gitlab_email_from'] = 'user@yourdomain.com'
 gitlab_rails['gitlab_email_display_name'] = 'Gitlab'
 gitlab_rails['smtp_enable'] = true
 gitlab_rails['smtp_address'] = "mail.yourdomain.com"
 gitlab_rails['smtp_port'] = 587
 gitlab_rails['smtp_user_name'] = "_username_"
 gitlab_rails['smtp_password'] = "_password_"
 gitlab_rails['smtp_domain'] = "yourdomain.com"
 gitlab_rails['smtp_authentication'] = "login"
 gitlab_rails['smtp_enable_starttls_auto'] = true
 gitlab_rails['smtp_tls'] = false
 gitlab_rails['smtp_openssl_verify_mode'] = 'none'

This settings need to be put in /etc/gitlab/gitlab.rb and then run gitlab-ctl reconfigure.

Step 3: Configure Dokku with Gitlab Container

Ok, you started a docker container in a server where exist Dokku.

This could be a problem if you want to expose your gitlab installation to port 80 (or 443) but you can easly configure dokku to support this.

In host machine go to:

cd /home/dokku/

Create a folder for our gitlab site:

mkdir gitlab
cd gitlab

Create a ngix.conf file:

nano nginx.conf

Put standard configuration for redirect all traffic to docker container:


server {
  listen      [::]:80;
  listen      80;
  server_name git.yourdomain.com;
  access_log  /var/log/nginx/git-access.log;
  error_log   /var/log/nginx/git-error.log;

  client_max_body_size 16M;

  location    / {
    gzip on;
    gzip_min_length  1100;
    gzip_buffers  4 32k;
    gzip_types    text/css text/javascript text/xml text/plain text/x-component application/javascript application/x-javascript application/json application/xml  application/rss+xml font/truetype application/$
    gzip_vary on;
    gzip_comp_level  6;

    proxy_pass  http://gitlab;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection upgrade;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Request-Start $msec;
  }
}
upstream gitlab {
  server 127.0.0.1:8080;
}

Now you can reload nginx:

service nginx reload

If you try to access to git.yourdomain.com (without specify any port) you should see your gitlab site.

Step 4: Upgrade to a new version

What happen if a new version of gitlab is available?

Gitlab doesn’t self update, so you have to do manually but is extremely easy.

Stop running container:

bash sudo docker stop gitlab

Remove existing container:

bash sudo docker rm gitlab

Pull new image:

bash sudo docker pull gitlab/gitlab-ce:latest

Create the container once again with previously specified options.
On the first run GitLab will reconfigure and update itself.

Step 4: Set backup

Backup is always a good thing, so it’s important that a copy of our repos is in a safe place.

Gitlab can backup all your repositories and upload to a remote storage like AWS S3.

This is the configuration that you have to set in /etc/gitlab/gitlab.rb for enable backup to S3 (remember to run gitlab-ctl reconfigure):

 gitlab_rails['manage_backup_path'] = true
 gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
 gitlab_rails['backup_keep_time'] = 604800
 gitlab_rails['backup_upload_connection'] = {
 'provider' => 'AWS',
 'region' => 'eu-west-1',
 'aws_access_key_id' => 'aws-key-id',
 'aws_secret_access_key' => 'aws-access-key'
 }
 gitlab_rails['backup_upload_remote_directory'] = 'your-bucket-name'
 gitlab_rails['backup_multipart_chunk_size'] = 104857600
 gitlab_rails['backup_encryption'] = 'AES256'

For try if backup works, you can run this command:

sudo gitlab-rake gitlab:backup:create

If all is ok, you should see your new backup in aws bucket.

Now we need to schedule a cron job that backs up our repositories and GitLab metadata. Use the root user:

sudo su -
crontab -e

There, add the following line to schedule the backup for everyday at 2 AM:

0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create
Posted in Linux, News

2 Comments

Leave a Reply

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