Skip to content
sentisso edited this page Oct 4, 2023 · 13 revisions

Notes

  1. There's going to be a few individual docker-compose-__.yml file examples. So you can either continuously merge them into one final docker-compose.yml file or keep them separated. Which ever suits you.
  2. used constants:
    • /opt/gitlab/ - I'll assume that all the configuration files and/or installed stuff will be at the "root" folder /opt/gitlab/, because it seems like the right place to put all the configuration at. So you can either choose your own "root" folder or just accept this one.
    • mydomain.com - this domain name will be used as an example. You need to replace it everywhere with your own, already existing, domain name. The steps that include mydomain.com will be marked with "(👷 contains mydomain.com)".
  3. Lines with the ➡️ emoji are the actual steps to take, everything else are just useful notes.

1. Install Docker (with compose)

1.1. Install Docker engine

➡️ https://docs.docker.com/engine/install/

1.2. Install Docker compose

➡️ https://docs.docker.com/compose/install/

1.3. Create a common Docker network gitlab

➡️ $ docker network create -d bridge gitlab

2. Setup nginx-proxy and SSL

This will allow you to easily expose the container services under different (sub)domain names on the same server (all under SSL thanks to Let's encrypt).

Take note of the vhost volume at the bottom of the compose file. This syncs the local folder /opt/gitlab/nginx/vhost.d/ with the nginx containers. This allows for easy use of custom per-VIRTUAL_HOST nginx configuration if needed.

➡️ docker-compose-nginx-proxy.yml
➡️ $ docker compose up -d

Sources:

3. Install GitLab using Docker compose

Well, here we go...

3.0. Prepare DNS records for the GitLab server (👷 contains mydomain.com)

This step needs to happen before running the GitLab server, so there are no issues with Let's encrypt.

➡️ Create an A record for the domain gitlab.mydomain.com
➡️ Create an A record for the domain registry-gitlab.mydomain.com

3.1. Set up the volumes location

Basically just create the appropriate folders for the GitLab server in your FS, that's the /data, /logs and /config folders in the $GITLAB_HOME.

➡️ https://docs.gitlab.com/ee/install/docker.html#set-up-the-volumes-location

3.2. Install GitLab using Docker Compose (👷 contains mydomain.com)

This docker-compose file also includes the configuration for Container registry.
Note: the used $GITLAB_HOME value is the same as in the previous step, so if you've used something other than /srv/gitlab/, then edit the compose file accordingly.

➡️ docker-compose-gitlab.yml
➡️ $ docker compose up -d

3.3. Check the public accessibility of the GitLab server

➡️ Just, you know... try to access it in your browser.

3.4. Get the root password

➡️ In the file initial_root_password (accessible via sudo cat /srv/gitlab/config/initial_root_password) should be the root password that you can use for logging in to the "root" account.

3.5. Configure sign-up restrictions

➡️ At /admin/application_settings/general#js-signup-settings you should disable new, unapproved sign-ups for your GitLab server.

3.6. Add SSH keys (👷 contains mydomain.com)

➡️ At /-/profile/keys add an SSH key that will allow you to actually push code to your GitLab server. See https://docs.gitlab.com/16.4/ee/user/ssh.html.
➡️ Add SSH config:

Host gitlab.mydomain.com
    HostName <your_server_IP>
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/gitlab_iusethisfortesting_sentisso

HostName can be left out if your DNS servers support the SSH protocol (for example Proxied Cloudflare does not).

3.7. Disable Auto DevOps

➡️ At /admin/application_settings/ci_cd#js-ci-cd-settings uncheck the "Default to Auto DevOps pipeline for all projects" option to disable Auto DevOps at the instance level.

3.8. Enable large body uploads (👷 contains mydomain.com)

➡️ Create the file /opt/gitlab/nginx/vhost.d/gitlab.mydomain.com with the following contents:

client_max_body_size 64m;

This is especially useful when you want to import projects from other sources or for large git pushes.

➡️ Create the file /opt/gitlab/nginx/vhost.d/registry-gitlab.mydomain.com with the following contents:

client_max_body_size 0;

This is needed when you want to take an advantage of the Container registry.

Sources:

4. Install and register GitLab runners

GitLab runners can be installed and ran on any server! Not just the root one, that hosts the GitLab server.

4.1. Install a new Docker GitLab runner

➡️ docker-compose-gitlab-runner.yml
➡️ $ docker compose up -d

4.2. Register the GitLab runner (👷 contains mydomain.com)

➡️ In the admin settings at /admin/runners/new "create" a new GitLab runner.
➡️ After submitting the form, remember/copy the runner token glrt-****, which is shown on the confirmation page.
➡️ On the server where you created the Docker GitLab runner, run docker compose exec gitlab-runner gitlab-runner register (where the first gitlab-runner is the container name of the runner). It will ask you for:

  1. the public URL of your GitLab server: https://gitlab.mydomain.com/,
  2. runner authentication token: that's the glrt-**** token from earlier,
  3. runner executor: use docker
  4. default image: use docker:latest

➡️ In the runner's configuration /srv/gitlab-runner/config/config.toml, edit the section [runners.docker]:

  1. Add "/var/run/docker.sock:/var/run/docker.sock:ro" to the volumes array,
  2. Add pull_policy = ["if_not_present"]
  3. Add network_mode = "host"
  4. Add priviliged = true
  5. Add a new service (as a subsection of [runners.docker]):
[[runners.docker.services]]
  name = "docker:dind"
  alias = "docker"

➡️ Restart the GitLab runner by $ docker compose exec gitlab-runner gitlab-runner restart

4.3. Check the availability of the runner

➡️ At /admin/runners it should have a status of "Online", including some information about the server where you deployed the runner at.

Sources:

Recommended next steps

If you've completed the previous steps, you can start developing and deploying new applications on your self-hosted GitLab server. The following sections are therefore optional, but are recommended and could be pretty useful.

Sources:

5. Configure SMTP

Configure SMTP for proper email notifications support.
The gitlab.rb file, that you should edit, is located at /srv/gitlab/config/gitlab.rb.

➡️ Follow the instructions for your email solution at https://docs.gitlab.com/omnibus/settings/smtp.html
➡️ Reconfigure the server by running docker compose exec gitlab gitlab-ctl reconfigure

CI/CD examples

In the examples/ folder, you will find a set (or just one lol) of CI/CD examples showcasing different scenarios and deployment methodologies that can be utilized on the GitLab server you've just set up.