Skip to content

Commit 8a965df

Browse files
committed
Adding Docker-in-Docker gitlab-runners to the setup also
1 parent 33f6cae commit 8a965df

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

README.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,41 @@ Before we´re able to register the Runner, we need to extract the Registration T
576576
register: gitlab_runner_registration_token_result
577577
```
578578

579-
The last step then is to register the Gitlab Docker Runner in [non-interactive mode](https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/commands/README.md#non-interactive-registration).
579+
As [the docs state](https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#runner-configuration):
580580

581+
> There are three methods to enable the use of docker build and docker run during jobs; each with their own tradeoffs.
581582
583+
As "The simplest approach is to install GitLab Runner in shell execution mode", we use the `shell` executor for our setup primarily:
584+
585+
To register the Gitlab Docker Runner in [non-interactive mode](https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/commands/README.md#non-interactive-registration), we do the following inside our playbook:
586+
587+
```
588+
# see https://docs.gitlab.com/ce/ci/docker/using_docker_images.html#register-docker-runner
589+
# and this for non-interactive mode:
590+
# https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/commands/README.md#non-interactive-registration
591+
- name: Register Gitlab-Runners using shell executor
592+
shell: "gitlab-runner register --non-interactive --url '{{gitlab_url}}' --registration-token '{{gitlab_runner_registration_token}}' --description 'shell-runner-{{ item }}' --executor shell"
593+
loop: "{{ range(1,gitlab_runner_count + 1)|list }}"
594+
```
595+
596+
__Attention!__ Do not confuse these runner configurations with the "Non-Docker-in-Docker" gitlab-runner also named "docker"!
597+
598+
If you don't want to go with the flexible and locally testable solution using a Dockerfile and docker commands directly inside your `.gitlab-ci.yml` (be aware of the fact, that you can't develop your pipeline locally right now because of the missing pieces in the `gitlab-runner exec` implementation! (see https://gist.github.com/jonashackt/2cfbf366a6a6b70a78068ab043edb8f7 for details)), then there's another - sadly widly used - way of how to register GitLab runners described here: https://docs.gitlab.com/ce/ci/docker/using_docker_images.html#register-docker-runner __But as with its predecessors like Jenkins, GitLab must not be the goto CI solution in the future - and if you want to be able to change your CI system fast, I would advice you to NOT USE this way of GitLab CI!__.
599+
600+
#### Configure a Docker-in-Docker enabled gitlab-runner with the docker executor
601+
602+
The second option on how to use standard Docker commands inside your `.gitlab-ci.yml`, is to use Docker-in-Docker (Dind) gitlab-runners - see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-workflow-with-docker-executor
603+
604+
Therefore we register our Dind runner like this - [incl. TLS enablement](https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#tls-enabled) mounting the host certs therefore with ` --docker-volumes '/certs/client'` and as stated in the docs we also pin to `--docker-image 'docker:19.03.1'` the Docker version to prevent "unpredictable behavior, especially when new versions are released".
605+
606+
A downside of the Docker-in-Docker approach is also the usage of `--docker-privileged`, which can lead to security implications because we disable the security mechanisms of containers:
607+
608+
```
609+
- name: Register Gitlab-Runners using docker executor too
610+
shell: "gitlab-runner register --non-interactive --url '{{gitlab_url}}' --registration-token '{{gitlab_runner_registration_token}}' --description 'docker-in-docker-runner-{{ item }}' --executor docker --docker-image 'docker:19.03.1' --docker-privileged --docker-volumes '/certs/client'"
611+
loop: "{{ range(1,gitlab_runner_count + 1)|list }}"
612+
613+
```
582614

583615

584616

Vagrantfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Vagrant.configure("2") do |config|
1313
config.vm.provider :virtualbox do |virtualbox|
1414
virtualbox.name = "gitlab-ci-stack"
1515
#virtualbox.gui = true
16-
virtualbox.memory = 4096
16+
virtualbox.memory = 3072
1717
virtualbox.cpus = 2
1818

1919
# Forward DNS resolver from host (vagrant dns) to box

gitlab-runner.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@
4141
append: yes
4242
become: true
4343

44-
# see https://docs.gitlab.com/ce/ci/docker/using_docker_images.html#register-docker-runner
44+
# see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-shell-executor
4545
# and this for non-interactive mode:
4646
# https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/commands/README.md#non-interactive-registration
4747
- name: Register Gitlab-Runners using shell executor
4848
shell: "gitlab-runner register --non-interactive --url '{{gitlab_url}}' --registration-token '{{gitlab_runner_registration_token}}' --description 'shell-runner-{{ item }}' --executor shell"
4949
loop: "{{ range(1,gitlab_runner_count + 1)|list }}"
5050

51+
# see https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-workflow-with-docker-executor
52+
- name: Register Gitlab-Runners using docker executor too
53+
shell: "gitlab-runner register --non-interactive --url '{{gitlab_url}}' --registration-token '{{gitlab_runner_registration_token}}' --description 'docker-in-docker-runner-{{ item }}' --executor docker --docker-image 'docker:19.03.1' --docker-privileged --docker-volumes '/certs/client'"
54+
loop: "{{ range(1,gitlab_runner_count + 1)|list }}"
55+
5156
# number of concurrent runners needs to be set directly in config-file in order to actually run jobs in parallel
5257
# see: https://gitlab.com/gitlab-org/gitlab-runner/issues/1539
5358
- name: set concurrent number of runners in gitlab-runner config

prepare-gitlab.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
gitlab_domain: "gitlab.jonashackt.io"
77
gitlab_url: "https://{{ gitlab_domain }}"
88
gitlab_registry_url: "{{ gitlab_url }}:4567"
9-
gitlab_runner_count: 5
9+
gitlab_runner_count: 2
1010
gitlab_pages_domain: "pages.jonashackt.io"
1111
gitlab_pages_url: "https://{{ gitlab_pages_domain }}"
1212

0 commit comments

Comments
 (0)