Technext
Technext

Reputation: 8107

GitLab CI - Cannot connect to the Docker daemon from within an image

I have a node-based project and following are the first few steps that are required to be executed as part of the build:

npm install
npm run build
docker build -t client .

The last command above builds the following Dockerfile:

FROM docker.artifactory.abc.net/nginx
COPY build /usr/share/nginx/html
COPY default.conf /etc/nginx/conf.d/default.conf

Content of .gitlab-ci.yml:

image: docker.artifactory.abc.net/docker/node:1.0

stages:
   - build
   - deploy

build:
   stage: build
   script:
   - npm install
   - npm run build
   - docker build -t client .

In the above Dockerfile, i am using a custom node image (node:1.0) which contains the proxy settings for apk to work and Artifactory configuration so all the dependencies are fetched using Artifactory. Now when i was running this build, i was getting docker: command not found error while executing the last command (docker build -t client .), which is expected because the base image is for node and doesn't contain docker. So i added docker setup instructions to the node Dockerfile based on this link except for the last 3 lines where it's configuring the ENTRYPOINT and CMD.

Now when i ran the build, i got:

$ docker build -t client .
Sending build context to Docker daemon  372.7MB

Step 1 : FROM docker.artifactory.abc.net/nginx
Get https://docker.artifactory.abc.net/v2/nginx/manifests/latest: unknown: Authentication is required
ERROR: Job failed: exit code 1

This error, as per my past experience, had to do with running docker login command. Since the docker setup in official image uses tar, i had to add docker user to /etc/group and then add current user (root) to the docker group. Also added the docker login command as shown below to the Dockerfile:

    addgroup docker; \
    adduser root docker; \
    docker login docker.artifactory.abc.net -u svc-art -p "ZTg6#&kq"; \

After that, if i try building this Dockerfile, i get following error:

+ dockerd -v
Docker version 17.05.0-ce, build v17.05.0-ce
+ docker -v
Docker version 17.05.0-ce, build v17.05.0-ce
+ adduser root docker
+ tail -2 /etc/group
node:x:1000:node
docker:x:101:root
+ docker login docker.artifactory.abc.net -u svc-art -p ZTg6#&kq
Warning: failed to get default registry endpoint from daemon (Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?). Using system default: https://index.docker.io/v1/
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

I also did an ls -ltr /var/run/docker.sock; and the docker socket file was not present inside the image. This seems to be the issue.

Any idea how i can get this working?

Upvotes: 1

Views: 2456

Answers (1)

Sergiu
Sergiu

Reputation: 3185

Well from the example you have provided I cannot see where you call your docker service, therefore I assume you are not calling it also you are not logging into the registry.

The way your pipeline should look like is something as follows:

 image: docker.artifactory.abc.net/docker/node:1.0

 stages:
    - build
    - deploy

 build:
   image: docker:latest
   services:
   - docker:dind
   stage: build
   script:
     - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN registry.example.com
     - docker build -t registry.example.com/group/project/image:latest .
     - docker push registry.example.com/group/project/image:latest

You could also find more info here

Upvotes: 1

Related Questions