Sign inSign up

lanycrost/gitlab

By lanycrost

Updated almost 8 years ago

This Container uses a customized Alpine Linux base which includes s6 overlay.

Image
0

1.1K

lanycrost/gitlab repository overview

lanycrost/gitlab:latest

Build Status Docker Pulls Docker Stars Docker Layers

Introduction

Dockerfile to build a GitLab Community Edition image for the Docker opensource container platform.

GitLab EE is set up in the Docker image using the install from source method as documented in the the official GitLab documentation.

This Container uses a customized Alpine Linux base which includes s6 overlay enabled for PID 1 Init capabilities. Cron also installed along with other tools (bash, curl, less, vim) for easier management.

For other methods to install GitLab please refer to the Official GitLab Installation Guide which includes a GitLab image for Docker.

Prerequisites

Your docker host needs to have 1GB or more of available RAM to run GitLab. Please refer to the GitLab hardware requirements documentation for additional information.

Installation

Automated builds of the image are available on Docker Hub and is the recommended method of installation.

docker pull lanycrost/gitlab:latest

You can also pull the latest tag which is built from the repository HEAD

docker pull lanycrost/gitlab:latest

Alternatively you can build the image locally.

docker build -t lanycrost/gitlab github.com/lanycrost/docker-gitlab

Quick Start

The quickest way to get started is using docker-compose.

wget https://raw.githubusercontent.com/lanycrost/docker-gitlab/master/examples/docker-compose.yml

Generate random strings that are at least 64 characters long for each of GITLAB_SECRETS_OTP_KEY_BASE, GITLAB_SECRETS_DB_KEY_BASE, and GITLAB_SECRETS_SECRET_KEY_BASE. These values are used for the following:

  • GITLAB_SECRETS_OTP_KEY_BASE is used to encrypt 2FA secrets in the database. If you lose or rotate this secret, none of your users will be able to log in using 2FA.
  • GITLAB_SECRETS_DB_KEY_BASE is used to encrypt CI secret variables, as well as import credentials, in the database. If you lose or rotate this secret, you will not be able to use existing CI secrets.
  • GITLAB_SECRETS_SECRET_KEY_BASE is used for password reset links, and other 'standard' auth features. If you lose or rotate this secret, password reset tokens in emails will reset.

Tip: You can generate a random string using pwgen -Bsv1 64 and assign it as the value of GITLAB_SECRETS_DB_KEY_BASE.

Start GitLab using:

docker-compose up

Alternatively, you can manually launch the gitlab container and the supporting postgres and redis containers by following this three step guide.

Step 1. Launch a postgres container

docker run --name gitlab-postgres -d \
    --env 'POSTGRES_DB=gitlabhq_production' \
    --env 'POSTGRES_USER=gitlab' --env 'POSTGRES_PASSWORD=password' \
    --volume /data/gitlab/postgresql:/var/lib/postgresql \
    postgres:10.5-alpine

Step 2. Launch a redis container

docker run --name gitlab-redis -d \
    --volume /data/gitlab/redis:/var/lib/redis \
    redis:latest

Step 3. Launch the gitlab container

docker run --name gitlab -d \
    --link gitlab-postgres:postgres --link gitlab-redis:redisio \
    --publish 10022:22 --publish 10080:80 \
    --env 'GITLAB_PORT=10080' --env 'GITLAB_SSH_PORT=10022' \
    --env 'GITLAB_SECRETS_DB_KEY_BASE=long-and-random-alpha-numeric-string' \
    --env 'GITLAB_SECRETS_SECRET_KEY_BASE=long-and-random-alpha-numeric-string' \
    --env 'GITLAB_SECRETS_OTP_KEY_BASE=long-and-random-alpha-numeric-string' \
    --env 'DB_ADAPTER=postgresql' --env 'DB_HOST=gitlab-postgres' \
    --env 'DB_NAME=gitlabhq_production' \
    --env 'DB_USER=gitlab' --env 'DB_PASS=password' \
    --volume /data/gitlab/gitlab:/home/git/data \
    lanycrost/gitlab:latest

Please refer to Available Configuration Parameters to understand GITLAB_PORT and other configuration options

NOTE: Please allow a couple of minutes for the GitLab application to start.

Point your browser to http://localhost:10080 and set a password for the root user account.

You should now have the GitLab application up and ready for testing. If you want to use this image in production then please read on.

The rest of the document will use the docker command line. You can quite simply adapt your configuration into a docker-compose.yml file if you wish to do so.

Configuration

Data Store

GitLab is a code hosting software and as such you don't want to lose your code when the docker container is stopped/deleted. To avoid losing any data, you should mount a volume at.

  • /home/git/data

Note that if you are using the docker-compose approach, this has already been done for you.

SELinux users are also required to change the security context of the mount point so that it plays nicely with selinux.

mkdir -p /data/gitlab/gitlab
sudo chcon -Rt svirt_sandbox_file_t /data/gitlab/gitlab

Volumes can be mounted in docker by specifying the -v option in the docker run command.

docker run --name gitlab -d \
    --volume /data/gitlab/gitlab:/home/git/data \
    lanycrost/gitlab:latest

Database

GitLab uses a database backend to store its data. You can configure this image to use either MySQL or PostgreSQL.

Note: GitLab HQ recommends using PostgreSQL over MySQL

PostgreSQL
External PostgreSQL Server

The image also supports using an external PostgreSQL Server. This is also controlled via environment variables.

CREATE ROLE gitlab with LOGIN CREATEDB PASSWORD 'password';
CREATE DATABASE gitlabhq_production;
GRANT ALL PRIVILEGES ON DATABASE gitlabhq_production to gitlab;

Additionally since GitLab 8.6.0 the pg_trgm extension should also be loaded for the gitlabhq_production database.

We are now ready to start the GitLab application.

Assuming that the PostgreSQL server host is 192.168.1.100

docker run --name gitlab -d \
    --env 'DB_ADAPTER=postgresql' --env 'DB_HOST=192.168.1.100' \
    --env 'DB_NAME=gitlabhq_production' \
    --env 'DB_USER=gitlab' --env 'DB_PASS=password' \
    --volume /data/gitlab/gitlab:/home/git/data \
    lanycrost/gitlab:latest
MySQL
External MySQL Server

The image can be configured to use an external MySQL database. The database configuration should be specified using environment variables while starting the GitLab image.

Before you start the GitLab image create user and database for gitlab.

CREATE USER 'gitlab'@'%.%.%.%' IDENTIFIED BY 'password';
CREATE DATABASE IF NOT EXISTS `gitlabhq_production` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
GRANT ALL PRIVILEGES ON `gitlabhq_production`.* TO 'gitlab'@'%.%.%.%';

We are now ready to start the GitLab application.

Assuming that the mysql server host is 192.168.1.100

docker run --name gitlab -d \
    --env 'DB_ADAPTER=mysql2' --env 'DB_HOST=192.168.1.100' \
    --env 'DB_NAME=gitlabhq_production' \
    --env 'DB_USER=gitlab' --env 'DB_PASS=password' \
    --volume /data/gitlab/gitlab:/home/git/data \
    lanycrost/gitlab:latest

Redis

GitLab uses the redis server for its key-value data store. The redis server connection details can be specified using environment variables.

External Redis Server

The image can be configured to use an external redis server. The configuration should be specified using environment variables while starting the GitLab image.

Assuming that the redis server host is 192.168.1.100

docker run --name gitlab -it --rm \
    --env 'REDIS_HOST=192.168.1.100' --env 'REDIS_PORT=6379' \
    lanycrost/gitlab:latest
Mail

The mail configuration should be specified using environment variables while starting the GitLab image. The configuration defaults to using gmail to send emails and requires the specification of a valid username and password to login to the gmail servers.

If you are using Gmail then all you need to do is:

docker run --name gitlab -d \
    --env '[email protected]' --env 'SMTP_PASS=PASSWORD' \
    --volume /data/gitlab/gitlab:/home/git/data \
    lanycrost/gitlab:latest

Please refer the Available Configuration Parameters section for the list of SMTP parameters that can be specified.

Reply by email

GitLab adds support for commenting on issues by replying to emails.

To enable this feature you need to provide IMAP configuration parameters that will allow GitLab to connect to your mail server and read mails. Additionally, you may need to specify GITLAB_INCOMING_EMAIL_ADDRESS if your incoming email address is not the same as the IMAP_USER.

If your email provider supports email sub-addressing then you should add the +%{key} placeholder after the user part of the email address, eg. GITLAB_INCOMING_EMAIL_ADDRESS=reply+%{key}@example.com. Please read the documentation on reply by email to understand the requirements for this feature.

If you are using Gmail then all you need to do is:

docker run --name gitlab -d \
    --env '[email protected]' --env 'IMAP_PASS=PASSWORD' \
    --env 'GITLAB_INCOMING_EMAIL_ADDRESS=USER+%{key}@gmail.com' \
    --volume /data/gitlab/gitlab:/home/git/data \
    lanycrost/gitlab:latest

Please refer the Available Configuration Parameters section for the list of IMAP parameters that can be specified.

SSL

Access to the gitlab application can be secured using SSL so as to prevent unauthorized access to the data in your repositories. While a CA certified SSL certificate allows for verification of trust via the CA, a self signed certificate can also provide an equal level of trust verification as long as each client takes some additional steps to verify the identity of your website. I will provide instructions on achieving this towards the end of this section.

Jump to the Using HTTPS with a load balancer section if you are using a load balancer such as hipache, haproxy or nginx.

To secure your application via SSL you basically need two things:

  • Private key (.key)
  • SSL certificate (.crt)

When using CA certified certificates, these files are provided to you by the CA. When using self-signed certificates you need to generate these files yourself. Skip to Strengthening the server security section if you are armed with CA certified SSL certificates.

Generation of a Self Signed Certificate

Generation of a self-signed SSL certificate involves a simple 3-step procedure:

STEP 1: Create the server private key

openssl genrsa -out gitlab.key 2048

STEP 2: Create the certificate signing request (CSR)

openssl req -new -key gitlab.key -out gitlab.csr

STEP 3: Sign the certificate using the private key and CSR

openssl x509 -req -days 3650 -in gitlab.csr -signkey gitlab.key -out gitlab.crt

Congratulations! You now have a self-signed SSL certificate valid for 10 years.

Installation of the SSL Certificates

Out of the four files generated above, we need to install the gitlab.key, gitlab.crt and dhparam.pem files at the gitlab server. The CSR file is not needed, but do make sure you safely backup the file (in case you ever need it again).

The default path that the gitlab application is configured to look for the SSL certificates is at /home/git/data/certs, this can however be changed using the SSL_KEY_PATH, SSL_CERTIFICATE_PATH and SSL_DHPARAM_PATH configuration options.

If you remember from above, the /home/git/data path is the path of the data store, which means that we have to create a folder named certs/ inside /data/gitlab/gitlab/ and copy the files into it and as a measure of security we'll update the permission on the gitlab.key file to only be readable by the owner.

mkdir -p /data/gitlab/gitlab/certs
cp gitlab.key /data/gitlab/gitlab/certs/
cp gitlab.crt /data/gitlab/gitlab/certs/
cp dhparam.pem /data/gitlab/gitlab/certs/
chmod 400 /data/gitlab/gitlab/certs/gitlab.key

Great! we are now just one step away from having our application secured.

Enabling HTTPS support

HTTPS support can be enabled by setting the GITLAB_HTTPS option to true. Additionally, when using self-signed SSL certificates you need to the set SSL_SELF_SIGNED option to true as well. Assuming we are using self-signed certificates

docker run --name gitlab -d \
    --publish 10022:22 --publish 10080:80 --publish 10443:443 \
    --env 'GITLAB_SSH_PORT=10022' --env 'GITLAB_PORT=10443' \
    --env 'GITLAB_HTTPS=true' --env 'SSL_SELF_SIGNED=true' \
    --volume /data/gitlab/gitlab:/home/git/data \
    lanycrost/gitlab:latest

In this configuration, any requests made over the plain http protocol will automatically be redirected to use the https protocol. However, this is not optimal when using a load balancer.

Configuring HSTS

HSTS if supported by the browsers makes sure that your users will only reach your sever via HTTPS. When the user comes for the first time it sees a header from the server which states for how long from now this site should only be reachable via HTTPS - that's the HSTS max-age value.

With NGINX_HSTS_MAXAGE you can configure that value. The default value is 31536000 seconds. If you want to disable a already sent HSTS MAXAGE value, set it to 0.

docker run --name gitlab -d \
 --env 'GITLAB_HTTPS=true' --env 'SSL_SELF_SIGNED=true' \
 --env 'NGINX_HSTS_MAXAGE=2592000' \
 --volume /data/gitlab/gitlab:/home/git/data \
 lanycrost/gitlab:latest

If you want to completely disable HSTS set NGINX_HSTS_ENABLED to false.

Using HTTPS with a load balancer

Load balancers like nginx/haproxy/hipache talk to backend applications over plain http and as such the installation of ssl keys and certificates are not required and should NOT be installed in the container. The SSL configuration has to instead be done at the load balancer.

However, when using a load balancer you MUST set GITLAB_HTTPS to true. Additionally you will need to set the SSL_SELF_SIGNED option to true if self signed SSL certificates are in use.

With this in place, you should configure the load balancer to support handling of https requests. But that is out of the scope of this document. Please refer to Using SSL/HTTPS with HAProxy for information on the subject.

When using a load balancer, you probably want to make sure the load balancer performs the automatic http to https redirection. Information on this can also be found in the link above.

In summation, when using a load balancer, the docker command would look for the most part something like this:

docker run --name gitlab -d \
    --publish 10022:22 --publish 10080:80 \
    --env 'GITLAB_SSH_PORT=10022' --env 'GITLAB_PORT=443' \
    --env 'GITLAB_HTTPS=true' --env 'SSL_SELF_SIGNED=true' \
    --volume /data/gitlab/gitlab:/home/git/data \
    lanycrost/gitlab:latest

Again, drop the --env 'SSL_SELF_SIGNED=true' option if you are using CA certified SSL certificates.

In case GitLab responds to any kind of POST request (login, OAUTH, changing settings etc.) with a 422 HTTP Error, consider adding this to your reverse proxy configuration:

proxy_set_header X-Forwarded-Ssl on; (nginx format)

Establishing trust with your server

This section deals will self-signed ssl certificates. If you are using CA certified certificates, your done.

This section is more of a client side configuration so as to add a level of confidence at the client to be 100 percent sure they are communicating with whom they think they.

This is simply done by adding the servers certificate into their list of trusted certificates. On ubuntu, this is done by copying the gitlab.crt file to /usr/local/share/ca-certificates/ and executing update-ca-certificates.

Again, this is a client side configuration which means that everyone who is going to communicate with the server should perform this configuration on their machine. In short, distribute the gitlab.crt file among your developers and ask them to add it to their list of trusted ssl certificates. Failure to do so will result in errors that look like this:

git clone https://git.local.host/gitlab-ce.git
fatal: unable to access 'https://git.local.host/gitlab-ce.git': server certificate verification failed. CAfile: /etc/ssl/certs/ca-certificates.crt CRLfile: none

You can do the same at the web browser. Instructions for installing the root certificate for firefox can be found here. You will find similar options chrome, just make sure you install the certificate under the authorities tab of the certificate manager dialog.

There you have it, that's all there is to it.

Installing Trusted SSL Server Certificates

If your GitLab CI server is using self-signed SSL certificates then you should make sure the GitLab CI server certificate is trusted on the GitLab server for them to be able to talk to each other.

The default path image is configured to look for the trusted SSL certificates is at /home/git/data/certs/ca.crt, this can however be changed using the SSL_CA_CERTIFICATES_PATH configuration option.

By default, our own server certificate gitlab.crt is added to the trusted certificates list.

Deploy to a subdirectory (relative url root)

By default GitLab expects that your application is running at the root (eg. /). This section explains how to run your application inside a directory.

Let's assume we want to deploy our application to '/git'. GitLab needs to know this directory to generate the appropriate routes. This can be specified using the GITLAB_RELATIVE_URL_ROOT configuration option like so:

docker run --name gitlab -it --rm \
    --env 'GITLAB_RELATIVE_URL_ROOT=/git' \
    --volume /data/gitlab/gitlab:/home/git/data \
    lanycrost/gitlab:latest

GitLab will now be accessible at the /git path, e.g. http://www.example.com/git.

Note: The GITLAB_RELATIVE_URL_ROOT parameter should always begin with a slash and SHOULD NOT have any trailing slashes.

OmniAuth Integration

GitLab leverages OmniAuth to allow users to sign in using Twitter, GitHub, and other popular services. Configuring OmniAuth does not prevent standard GitLab authentication or LDAP (if configured) from continuing to work. Users can choose to sign in using any of the configured mechanisms.

Refer to the GitLab documentation for additional information.

CAS3

To enable the CAS OmniAuth provider you must register your application with your CAS instance. This requires the service URL GitLab will supply to CAS. It should be something like: https://git.example.com:443/users/auth/cas3/callback?url. By default handling for SLO is enabled, you only need to configure CAS for backchannel logout.

For example, if your cas server url is https://sso.example.com, then adding --env 'OAUTH_CAS3_SERVER=https://sso.example.com' to the docker run command enables support for CAS3 OAuth. Please refer to Available Configuration Parameters for additional CAS3 configuration parameters.

Authentiq

To enable the Authentiq OmniAuth provider for passwordless authentication you must register an application with Authentiq. Please refer to the GitLab documentation for the procedure to generate the client ID and secret key with Authentiq.

Once you have the API client id and client secret generated, configure them using the OAUTH_AUTHENTIQ_CLIENT_ID and OAUTH_AUTHENTIQ_CLIENT_SECRET environment variables respectively.

For example, if your API key is xxx and the API secret key is yyy, then adding --env 'OAUTH_AUTHENTIQ_CLIENT_ID=xxx' --env 'OAUTH_AUTHENTIQ_CLIENT_SECRET=yyy' to the docker run command enables support for Authentiq OAuth.

You may want to specify OAUTH_AUTHENTIQ_REDIRECT_URI as well. The OAuth scope can be altered as well with OAUTH_AUTHENTIQ_SCOPE (defaults to 'aq:name email~rs address aq:push').

Google

To enable the Google OAuth2 OmniAuth provider you must register your application with Google. Google will generate a client ID and secret key for you to use. Please refer to the GitLab documentation for the procedure to generate the client ID and secret key with google.

Once you have the client ID and secret keys generated, configure them using the OAUTH_GOOGLE_API_KEY and OAUTH_GOOGLE_APP_SECRET environment variables respectively.

For example, if your client ID is

Tag summary

Content type

Image

Digest

Size

1.4 GB

Last updated

almost 8 years ago

docker pull lanycrost/gitlab