Example GitLab Runner docker compose configuration

After having spent some time the other day to redo my (“standalone”) runner setup, I figured that I would share this for the benefit of the community.

.env:

RUNNER_NAME=RUNNER-NAME
REGISTRATION_TOKEN=TOKEN
CI_SERVER_URL=https://gitlab.com/

docker-compose.yml:

version: "3.5"

services:
  dind:
    image: docker:20-dind
    restart: always
    privileged: true
    environment:
      DOCKER_TLS_CERTDIR: ""
    command:
      - --storage-driver=overlay2

  runner:
    restart: always
    image: registry.gitlab.com/gitlab-org/gitlab-runner:alpine
    depends_on:
      - dind
    environment:
      - DOCKER_HOST=tcp://dind:2375
    volumes:
      - ./config:/etc/gitlab-runner:z

  register-runner:
    restart: 'no'
    image: registry.gitlab.com/gitlab-org/gitlab-runner:alpine
    depends_on:
      - dind
    environment:
      - CI_SERVER_URL=${CI_SERVER_URL}
      - REGISTRATION_TOKEN=${REGISTRATION_TOKEN}
    command:
      - register
      - --non-interactive
      - --locked=false
      - --name=${RUNNER_NAME}
      - --executor=docker
      - --docker-image=docker:20-dind
      - --docker-volumes=/var/run/docker.sock:/var/run/docker.sock
    volumes:
      - ./config:/etc/gitlab-runner:z

Source: TyIsI / gitlab-runner-docker-compose · GitLab

Notes:

  • Because networks are not specified, docker-compose will create project network, which defaults to bridge mode.
  • The docker references can be a little bit confusing. So let me try to clear that up:
    • By setting DOCKER_TLS_CERTDIR to empty, the dind instance is forced to use plain TCP
    • The runner connects to dind over TCP.
    • The docker.sock referenced in the register-runner is in reference to the dind executor. It’s how the containers in the docker container can talk to docker.
  • This config runs fully in dind.
  • This config is based on a config that I found and then optimized (and brought up to docker 20).
  • The config in the repo does some filesystem caching.

Hope this helps anyone!

5 Likes

I was struggling for hours in order to have a full gitlab ci/cd-> docker-compose runner configuration up and running with no luck till now.
At a certain point it was clear to me that i needed to configure a dind service for my docker runner but i was unable to make it work correctly.
Now i ran your docker-compose and its working like a charm!
Thanks man!

1 Like