Contents

Gitlab Kubernetes runner with Docker in Docker

Docker in Docker (DIND) is a powerful tool for running Docker containers within Docker containers. This allows developers to easily create and test Docker images within a container environment that closely resembles the production environment. This can help to catch issues early in the development process and ensure that the application is running as expected in the production environment

DinD config for kubernetes runner

All examples stored in repo

Register and deployGitlab Runner

  1. Create new runner in Gitlab UI [USER] > [REPO] > [CI/CD Settings] > [New runner]. Save <token>, and assign tags (for example dind to use it later)
  2. Deploy the runner to k8s
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    kubectl create ns gitlab
    helm repo add gitlab https://charts.gitlab.io/
    helm repo update
    
    helm upgrade -i gitlab-runner \
        --set gitlabUrl=https://gitlab.com,runnerRegistrationToken=<token> \
        gitlab/gitlab-runner \
        -n gitlab \
        -f values.yaml
    

Gitlab job manifest

Gitlab needs additional variables like DOCKER_HOST, DOCKER_TLS_CERTDIR, see working example below

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
docker-compose:
  image: docker:latest
  stage: test
  services:
    - docker:dind
  variables:
    # Enable log output for service container
    CI_DEBUG_SERVICES: "true"
    # Instruct Testcontainers to use the daemon of DinD, use port 2375 for non-tls connections.
    DOCKER_HOST: "tcp://docker:2375"
    # Improve performance with overlayfs.
    DOCKER_DRIVER: "overlay2"
    # Instruct Docker not to start over TLS.
    DOCKER_TLS_CERTDIR: ""
  script:
    - |
            docker-compose up
  tags:
    - dind