从GitLab CI运行程序连接到docker-in-docker [英] Connecting to docker-in-docker from a GitLab CI runner
问题描述
我有一个想要的GitLab管道:
I have a GitLab pipeline that I want to:
- 构建Java应用
- 使用docker-compose测试
- 推送到我的Docker存储库
我遇到的主要问题是有效:
services:
- docker:dind
docker_test:
stage: docker_test
image: docker:latest
script:
- docker version
输出将按预期打印:
> 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
这不是 (省略了 docker-ce
的安装步骤):
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
它失败并显示:
$ 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
如何使我的 ubuntu
映像(或用于构建项目的任何映像)连接到链接的Docker-in-Docker服务?不是我的 docker:latest
是什么?
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?
我已经阅读了 GitLab服务文档,但这仅对主机名有意义.(如果有 mysql
服务,则可以通过 mysql:3306
连接.)
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
.)
将命令更新为 echo $ DOCKER_HOST
,我在 docker:latest
图像中看到:
Updating the command to echo $DOCKER_HOST
, I see in the docker:latest
image:
$ echo $DOCKER_HOST
tcp://docker:2375
在 ubuntu:latest
图片中,我看到:
$ echo $DOCKER_HOST
(nothing - but SO doesn't let me add a blank code line)
推荐答案
作为您添加的信息,我希望它能起作用:
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
或者:
services:
- docker:dind
docker_test:
stage: docker_test
image: ubuntu:latest
script:
- export DOCKER_HOST=tcp://docker:2375
- docker version
Gitlab似乎没有为自定义图像设置DOCKER_HOST变量.
It seems that Gitlab does not set the DOCKER_HOST variable for custom images.
这篇关于从GitLab CI运行程序连接到docker-in-docker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!