从 GitLab CI 运行器连接到 docker-in-docker [英] Connecting to docker-in-docker from a GitLab CI runner

查看:11
本文介绍了从 GitLab CI 运行器连接到 docker-in-docker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想要的 GitLab 管道:

I have a GitLab pipeline that I want to:

  1. 构建 Java 应用
  2. 使用 docker-compose 进行测试
  3. 推送到我的 Docker 存储库

我遇到的主要问题是这有效:

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆