can not run docker latest on gitlab-ci runner

2019-07-13 18:05发布

I'm testing gitlab-ci and trying to generate an image on the registry from the Dockerfile.

I have the same code just to test:

#gitlab-ci 
image: docker:latest

tages:
  - build
  - deploy

build_application:
  stage: build
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA . -f Dockerfile
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-test

output:

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

docker is running the image is being pulled but I can not execute docker commands.

In my local environment if a run:

docker run -it docker:latest

I stay inside the container and run docker info i have the same problem. I had to fix it by running the container on this way:

docker run -it --rm -v /var/run/docker.sock:/var/run/docker.sock docker:latest

but I do not know how to fix it on gitlab-ci. I configured my runner so:

docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

Maybe someone can put me in the right direction. thanks

1条回答
地球回转人心会变
2楼-- · 2019-07-13 18:37

By default it is not possible to run docker-in-docker (DIND) (as a security measure).

This section in the Gitlab docs is your solution. You must use Docker-in-Docker.

After configuring your runner to use DIND your .gitlab-ci.yml will look like this:

#gitlab-ci 
image: docker:latest

variables:
  DOCKER_DRIVER: overlay2

services:
- docker:dind

before_script:
- docker info

stages:
  - build
  - deploy

build_application:
  stage: build
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA . -f Dockerfile
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA-test
查看更多
登录 后发表回答