Docker Build Fails - Gitlab CI with GKE. Cannot connect to the Docker daemon at tcp://localhost:2375. Is the docker daemon running?

11/14/2018

I Integrated Google Kubernetes Engine with my Gitlab repo and created a cluster from gitlab. Now I am trying to build my docker image using gitlab-ci and push it to Google Container Registry. But I keep getting this error:

Running with gitlab-runner 11.2.0 (35e8515d)
  on gitlab runner vm instance 4e6e33ed
Using Docker executor with image docker:latest ...
Starting service docker:dind ...
Pulling docker image docker:dind ...
Using docker image     sha256:edbe3f3ad406799b528fe6633c5553725860566b638cdc252e0520010436869f for docker:dind ...
Waiting for services to be up and running...

*** WARNING: Service runner-4e6e33ed-project-8016623-concurrent-0-docker-0 probably didn't start properly.

Health check error:
ContainerStart: Error response from daemon: Cannot link to a non running container: /runner-4e6e33ed-project-8016623-concurrent-0-docker-0 AS /runner-4e6e33ed-project-8016623-concurrent-0-docker-0-wait-for-service/service (executor_docker.go:1305:0s)

Service container logs:
2018-11-14T13:02:37.917684152Z mount: permission denied (are you root?)
2018-11-14T13:02:37.917743944Z Could not mount /sys/kernel/security.
2018-11-14T13:02:37.917747902Z AppArmor detection and --privileged mode might break.
2018-11-14T13:02:37.917750733Z mount: permission denied (are you root?)

*********

Pulling docker image docker:latest ...
Using docker image sha256:062267097b77e3ecf374b437e93fefe2bbb2897da989f930e4750752ddfc822a for docker:latest ...
Running on runner-4e6e33ed-project-8016623-concurrent-0 via gitlab-runners..

###
# Running before_script commands here
###

# Error Comes on Docker build command
Cannot connect to the Docker daemon at tcp://localhost:2375. Is the docker daemon running?
ERROR: Job failed: exit code 1

This is my gitlab-ci.yml.

services:
- docker:dind

before_script:
- apk update && apk upgrade && apk add --no-cache bash openssh

variables:
DOCKER_DRIVER: overlay2

stages:
- build

build:
stage: build
image: docker:latest
variables:
    DOCKER_HOST: tcp://localhost:2375
before_script:
# Pre-requisites required to install google cloud sdk on gitlab runner
- export COMMIT_SHA=$(echo $CI_COMMIT_SHA | cut -c1-8)
- apk update
- apk upgrade
- apk add --update ca-certificates
- apk add --update -t deps curl
- apk del --purge deps
- rm /var/cache/apk/*
script:
    # Build our image using docker
    - docker build -t $GCP_PROJECT_ID/$CI_PROJECT_NAME:$COMMIT_SHA .

    # Write our GCP service account private key into a file
    - echo $GCLOUD_SERVICE_KEY | base64 -d > ${HOME}/gcloud-service-key.json

    # Download and install Google Cloud SDK
    - wget https://dl.google.com/dl/cloudsdk/release/google-cloud-sdk.tar.gz
    - tar zxvf google-cloud-sdk.tar.gz && ./google-cloud-sdk/install.sh --usage-reporting=false --path-update=true

    # Update gcloud components
    - google-cloud-sdk/bin/gcloud --quiet components update

    # Give access to gcloud project
    - google-cloud-sdk/bin/gcloud auth activate-service-account --key-file ${HOME}/gcloud-service-key.json || die "unable to authenticate service account for gcloud"

    # Get current projects credentials to access it
    - google-cloud-sdk/bin/gcloud container clusters get-credentials gitlab-kube --zone cluster-zone --project project-id

    # Configure container registry to push using docker
    - docker login -u _json_key --password-stdin https://gcr.io < ${HOME}/gcloud-service-key.json

    # Push the image using docker
    - docker push $GCP_PROJECT_ID/$CI_PROJECT_NAME:$COMMIT_SHA

The docker image is building on local. Also I saw on various posts to update the config.toml file but I dont have one in my project. Where to add that file?

Thanks

-- Manish Gupta
docker
gitlab
gitlab-ci
google-kubernetes-engine
kubernetes

2 Answers

3/16/2019

you should just set privilege = true in gitlab-runner config file, and restart it.

-- shaddock
Source: StackOverflow

11/19/2018

First of all : You don't need gcloud to push your images to GCP. The authentication by service account (like you do) is enough. (see: https://cloud.google.com/container-registry/docs/advanced-authentication#json_key_file)

However... If you really want to use the Gloud SDK, use the google/gcloud-sdk image instead of the docker image in your job (Docker is already present inside the google/gcloud-sdk image)

Next, to use the docker deamon, you need to specify the good endpoint. You use the docker:dind service so, the Docker Host will be tcp://docker:2375/ (docker is the hostname of your service)

Finally, your runner will need the "privilegied" mode (To do DIND). (See: https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor)

This is a quick example (sorry, not tested), to do that :

stages:
- build

build:
  stage: build
  image: google/cloud-sdk
  services:
  - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_HOST: tcp://docker:2375/
  before-script:
    - echo $GCLOUD_SERVICE_KEY | base64 -d > ${HOME}/gcloud-service-key.json
  script:
    - docker build -t $GCP_PROJECT_ID/$CI_PROJECT_NAME:$COMMIT_SHA .
    - docker login -u _json_key --password-stdin https://gcr.io < ${HOME}/gcloud-service-key.json
    - docker push $GCP_PROJECT_ID/$CI_PROJECT_NAME:$COMMIT_SHA
-- alphayax
Source: StackOverflow