我正在使用GitLab-CI通过docker-compose构建映像,并且gitlab-runner作为docker服务运行。为了保持从主机提取的作业,我将docker/compose
图像用作运行程序,将docker:dind
作为服务。
这是我的.gitlab-ci.yml
文件:
image:
name: docker/compose:1.25.0
entrypoint: ["/bin/sh", "-c"]
stages:
- build
- deploy
build-prod:
stage: build
tags:
- docker
- build
services:
- docker:18.09-dind
variables:
IMAGE_HOME: $CI_REGISTRY_IMAGE
DOCKER_HOST: tcp://docker:2375
DOCKER_DRIVER: overlay
before_script:
- docker version
- docker-compose version
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
script:
- docker-compose pull -q --ignore-pull-failures
- docker-compose build --parallel --pull
- docker image ls
- docker-compose push
和config.toml
(显然还有其他内容的<GITLAB_URL>
和<RUNNER_TOKEN>
):
[[runners]]
name = "docker-builder"
url = "<GITLAB_URL>"
token = "<RUNNER_TOKEN>"
executor = "docker"
[runners.custom_build_dir]
[runners.docker]
tls_verify = false
image = "docker:latest"
privileged = true
disable_entrypoint_overwrite = false
oom_kill_disable = false
disable_cache = false
volumes = ["/cache"]
shm_size = 0
[runners.cache]
[runners.cache.s3]
[runners.cache.gcs]
在before_script部分的第一行中,我得到以下输出(其中<MY_HOST_IP>
是服务器的IP地址):
$ docker version
Client: Docker Engine - Community
Version: 18.09.7
API version: 1.39
Go version: go1.10.8
Git commit: 2d0083d
Built: Thu Jun 27 17:54:15 2019
OS/Arch: linux/amd64
Experimental: false
error during connect: Get http://docker:2375/v1.39/version: dial tcp <MY_HOST_IP>:2375: connect: no route to host
ERROR: Job failed: exit code 1
运行docker/compose
映像的容器似乎正在将docker
解析为主机,而不是dind-service,但我不知道为什么。
它也与转轮容器本身无关,因为/etc/hosts
的内容看起来还不错,并且对docker服务执行ping操作也很好:
$ cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 docker b6bbd7a43b73 runner-G1yqwPKG-project-9-concurrent-0-docker-0
172.17.0.3 runner-G1yqwPKG-project-9-concurrent-0
$ ping -c 3 docker
PING docker (172.17.0.2): 56 data bytes
64 bytes from 172.17.0.2: seq=0 ttl=64 time=0.216 ms
64 bytes from 172.17.0.2: seq=1 ttl=64 time=0.154 ms
64 bytes from 172.17.0.2: seq=2 ttl=64 time=0.158 ms
--- docker ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.154/0.176/0.216 ms
尝试使用DOCKER_HOST: tcp://172.17.0.2:2375
进行构建时,一切工作均正常,并且构建成功。问题是我不能使用这种方法,因为在某些情况下,可能会在默认的docker-bridge中创建其他容器,或者正在运行多个构建。
我也完全感到困惑,因为我在另一个GitLab实例上使用相同的设置,其中所有设置几乎完全相同,除了主机之外,正在运行的主机在vm上在ubuntu:16.04上运行标准docker,而不是有效的方法是在服务器上本机在debian:10上使用docker swarm。
如果有人指出可能明显的错误,我将非常有帮助。