Connecting to docker-in-docker from a GitLab CI runner

瘦欲@ 提交于 2021-02-04 10:46:16

问题


I have a GitLab pipeline that I want to:

  1. Build a Java app
  2. Test using docker-compose
  3. Push to my Docker repository

The primary issue I'm having is that this works:

services:
  - docker:dind

docker_test:
  stage: docker_test
  image: docker:latest
  script:
  - docker version

The output is printed as expected:

> gitlab-ci-multi-runner exec docker --docker-privileged docker_test
...
$ docker version
Client:
 Version:      17.06.0-ce
...
Server:
 Version:      17.06.0-ce
...
Build succeeded

While this does not (installation steps for docker-ce omitted):

services:
  - docker:dind

docker_test:
  stage: docker_test
  image: ubuntu:latest       << note change
  script:
  - docker version

It fails with:

$ docker version
Client:
 Version:      17.06.0-ce
 API version:  1.30
 Go version:   go1.8.3
 Git commit:   02c1d87
 Built:        Fri Jun 23 21:23:31 2017
 OS/Arch:      linux/amd64
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
ERROR: Build failed: exit code 1
FATAL: exit code 1

How do I make my ubuntu image (or whatever image is going to build my project) connect to the linked Docker-in-Docker service? What is docker:latest doing that I'm not?

I've read up on the GitLab services documentation, but it only makes sense to me from a hostname perspective. (If you have a mysql service, you can connect over mysql:3306.)

Edit: Updating the command to echo $DOCKER_HOST, I see in the docker:latest image:

$ echo $DOCKER_HOST
tcp://docker:2375

And in the ubuntu:latest image I see:

$ echo $DOCKER_HOST
(nothing - but SO doesn't let me add a blank code line)

回答1:


As the information you've added, I hope that this does work:

services:
  - docker:dind

docker_test:
  stage: docker_test
  image: ubuntu:latest
  variables:
      DOCKER_HOST: "tcp://docker:2375"
  script:
  - docker version

Alternatively:

services:
  - docker:dind

docker_test:
  stage: docker_test
  image: ubuntu:latest 
  script:
  - export DOCKER_HOST=tcp://docker:2375
  - docker version

It seems that Gitlab does not set the DOCKER_HOST variable for custom images.




回答2:


The Gitlab CI runner needs to mount the hosts docker socket in order to excute docker in docker. For example:

docker exec gitlab-runner gitlab-runner register \
           --non-interactive \
           --url https://gitlab.com/ci \
           --registration-token TOKEN\
           --description "Docker Runner" \
           --tag-list "docker" \
           --executor docker \
           --docker-image "docker:latest" \
           --docker-volumes /var/run/docker.sock:/var/run/docker.sock

As you can see the docker.sock is mounted here as last parameter. However i would not recommend the use of docker-compose for Gitlab CI, since Gitlab CI has its own syntax https://docs.gitlab.com/ce/ci/docker/using_docker_images.html



来源:https://stackoverflow.com/questions/45316098/connecting-to-docker-in-docker-from-a-gitlab-ci-runner

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!