Custom Gitlab Runner Failing - Cannot connect to the Docker daemon

Viewed 1076

These are the steps I followed to setup a custom Gitlab runner:

  1. Install Gitlab runner by following the instructions here: https://docs.gitlab.com/runner/install/linux-repository.html

  2. Register a runner with docker exectuor and docker:19 image

  3. Write gitlab-ci.yml as follow:

image: docker:19.03.1

services:
  - name: docker:19.03.1-dind
    alias: docker

stages:
- build

build:
  stage: build
  variables:
    IMAGE_TAG: repo.azurecr.io/some-repo
    DOCKER_HOST: tcp://172.17.0.1:2375
    DOCKER_TLS_CERTDIR: ''
  script:
    - docker login someacr.azurecr.io -u "$SERVICE_PRINCIPAL_USER" -p "$SERVICE_PRINCIPAL_PASSWORD"
    - if [[ "$CI_COMMIT_REF_NAME" == "develop" ]]; then docker build -t $IMAGE_TAG .; fi
  1. Running the pipeline throws this error:
Login succeeded
if [[ "$CI_COMMIT_REF_NAME" == "develop" ]]; then docker build -t $IMAGE_TAG .; fi
Cannot connect to the Docker daemon at tcp://172.17.0.1:2375. Is the docker daemon running?
Cleaning up project directory and file based variables
00:02
ERROR: Job failed: exit code 1

This is what I have tried so far:

  1. privileged: true in /etc/gitlab-runner/config.toml
  2. dind:19 docker image in gitlab-ci.yml
  3. adding volumes = ["/var/run/docker.sock /var/run/docker.sock" "/cache"] in /etc/gitlab-runner/config.toml
  4. used DOCKER_TLS_CERTDIR: '' in gitlab-ci.yml as variable
  5. populating DOCKER_HOST variable with DOCKER_HOST: tcp://172.17.0.1:2375
  6. much more...

All these changes result in either of the 2 issues described in Gitlab Runner Troubleshooting here: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#troubleshooting

  1. docker: Cannot connect to the Docker daemon at tcp://docker:2375. Is the docker daemon running?
  2. Docker no such host error.
1 Answers

Your DOCKER_HOST variable value is not correct.

Use DOCKER_HOST: "tcp://docker:2375"

adding volumes = ["/var/run/docker.sock /var/run/docker.sock" "/cache"] in /etc/gitlab-runner/config.toml

I would recommend removing the volume mount of /var/run/docker.sock from the runner configuration here. This is not necessary when leveraging the docker:dind service container method. Though, it technically won't cause issues.

privileged: true in /etc/gitlab-runner/config.toml
[...]
used DOCKER_TLS_CERTDIR: '' in gitlab-ci.yml as variable

These changes are good, keep these changes.

Related