Cannot access docker daemon in gitlab runner using Kubernetes executor

11/14/2019

I setup my Gitlab instance using Kubernetes to deploy and also to do my CI using the Kubernetes cluster. Kubernetes is managed by Gitlab, so I did never touch the kubernetes by myself. Gitlab installed the four available packages: Helm Tiller Ingress, Cert-Manager, Prometheus and GitLab Runner.

I installed the Kubernetes cluster on a barebone server using the tutorial from: https://vitux.com/install-and-deploy-kubernetes-on-ubuntu/. The operating system of the server is Ubuntu 18.04 minimal. I found out, that Gitlab cannot install Helm Tiller on version 1.16 of Kubernetes so I installed the version 1.15.5-00 of Kubernetes on the server.

Problem:

I have a project, where I want to build a docker image. I try to use the dind service to build the docker image with the gitlab runner which is deployed on the kubernetes platform.

The build process fails with the following output:

Running with gitlab-runner 12.1.0 (de7731dd)
  on runner-gitlab-runner-699dc9bcc8-sgmcw -YPHFGCL
Using Kubernetes namespace: gitlab-managed-apps
Using Kubernetes executor with image docker:stable ...
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-0qj6sn to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-0qj6sn to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-0qj6sn to be running, status is Pending
Running on runner--yphfgcl-project-97-concurrent-0qj6sn via runner-gitlab-runner-699dc9bcc8-sgmcw...
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/sadion/ci-test/.git/
Created fresh repository.
From https://git.sadion.net/sadion/ci-test
 * [new branch]      master     -> origin/master
Checking out d179001c as master...

Skipping Git submodules setup
$ docker --version
Docker version 19.03.4, build 9013bf583a
$ docker build -t $TEST_NAME .
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
ERROR: Job failed: command terminated with exit code 1

Source files:

The Dockerfile I am using is pretty simple and is also valid, since I was able to build the image on my local machine:

FROM httpd:2.4
COPY ./index.html /usr/local/apache2/htdocs/

The .gitlab-ci.yml file I am using is:

image: docker:stable

variables:
  TEST_NAME: local/test

services:
  - docker:dind

stages:
  - build

before_script:
  - docker info

build_docker_image:
  stage: build
  before_script:
    - docker --version
  script:
    - docker build -t $TEST_NAME .
  tags:
    - build
    - kubernetes

Trying to export DOCKER_HOST

I also tried to export the DOCKER_HOST variable. But with that configured I get the same error:

image: docker:stable

variables:
  TEST_NAME: local/test
  DOCKER_HOST: tcp://localhost:2375

services:
  - docker:dind

stages:
  - build

before_script:
  - docker info

build_docker_image:
  stage: build
  before_script:
    - docker --version
  script:
    - docker build -t $TEST_NAME .
  tags:
    - build
    - kubernetes

The output of the runner is:

Running with gitlab-runner 12.1.0 (de7731dd)
  on runner-gitlab-runner-699dc9bcc8-sgmcw -YPHFGCL
Using Kubernetes namespace: gitlab-managed-apps
Using Kubernetes executor with image docker:stable ...
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-042nmk to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-042nmk to be running, status is Pending
Waiting for pod gitlab-managed-apps/runner--yphfgcl-project-97-concurrent-042nmk to be running, status is Pending
Running on runner--yphfgcl-project-97-concurrent-042nmk via runner-gitlab-runner-699dc9bcc8-sgmcw...
Fetching changes with git depth set to 50...
Initialized empty Git repository in /builds/sadion/ci-test/.git/
Created fresh repository.
From https://git.sadion.net/sadion/ci-test
 * [new branch]      master     -> origin/master
Checking out 57b6be1d as master...

Skipping Git submodules setup
$ docker --version
Docker version 19.03.4, build 9013bf583a
$ docker build -t $TEST_NAME .
Cannot connect to the Docker daemon at tcp://localhost:2375. Is the docker daemon running?
ERROR: Job failed: command terminated with exit code 1
-- user9573909
docker
gitlab
gitlab-ci
gitlab-ci-runner
kubernetes

1 Answer

11/14/2019

So I found the solution. I think the problem was that I did not specify the certificate directory. By changing the directory to "" it started to work.

image: docker:stable

variables:
  TEST_NAME: local/test
  DOCKER_HOST: tcp://localhost:2375
  DOCKER_TLS_CERTDIR: ""

services:
  - docker:dind

before_script:
  - docker info

build_docker_image:
  stage: build
  script:
    - docker build -t $TEST_NAME .
  tags:
    - build
    - kubernetes
    - test
-- user9573909
Source: StackOverflow