<p>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 <code>gitlab-ci</code> and push it to Google Container Registry.
But I keep getting this error:</p>

<pre><code>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
</code></pre>

<p>This is my gitlab-ci.yml.</p>

<pre><code>services:
- docker:dind

before_script:
- apk update &amp;&amp; apk upgrade &amp;&amp; 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 &gt; ${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 &amp;&amp; ./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 &lt; ${HOME}/gcloud-service-key.json

    # Push the image using docker
    - docker push $GCP_PROJECT_ID/$CI_PROJECT_NAME:$COMMIT_SHA
</code></pre>

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

<p>Thanks</p>

Manish Gupta
<p>First of all : You don't need <code>gcloud</code> to push your images to GCP. The authentication by service account (like you do) is enough. (see: <a href="https://cloud.google.com/container-registry/docs/advanced-authentication#json_key_file" rel="nofollow noreferrer">https://cloud.google.com/container-registry/docs/advanced-authentication#json_key_file</a>)</p>

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

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

<p>Finally, your runner will need the "privilegied" mode (To do DIND). (See: <a href="https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor" rel="nofollow noreferrer">https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor</a>)</p>

<p>This is a quick example (sorry, not tested), to do that :</p>

<pre><code>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 &gt; ${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 &lt; ${HOME}/gcloud-service-key.json
    - docker push $GCP_PROJECT_ID/$CI_PROJECT_NAME:$COMMIT_SHA
</code></pre>
