在 GitLab CI 管道中使用 docker-compose
Using docker-compose in a GitLab CI pipeline
我正在尝试使用以下 .gitlab-ci.yml
文件实施 GitLab 持续集成 (CI) 管道:
image: docker:latest
# When using dind, it's wise to use the overlayfs driver for
# improved performance.
variables:
DOCKER_DRIVER: overlay
services:
- docker:dind
before_script:
- docker info
- curl -L "https://github.com/docker/compose/releases/download/1.10.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose
- export PATH=/usr/local/bin:$PATH
- echo $PATH
stages:
- build
- test
build:
stage: build
script:
- docker-compose build
- docker-compose up -d
test:
stage: test
script:
- cd tests/tester
- pytest test_run_tester.py
except:
- /^.*skip_tests$/
但是,在 GitLab 中,我收到以下错误消息:
Running with gitlab-ci-multi-runner 1.10.4 (b32125f)
Using Docker executor with image docker:latest ...
Starting service docker:dind ...
Pulling docker image docker:dind ...
Waiting for services to be up and running...
Pulling docker image docker:latest ...
Running on runner-2e54fd37-project-13-concurrent-0 via scw-de9c9c...
Fetching changes...
HEAD is now at 2504a08 Update CI config
From https://lab.iperlane.com/gio/ipercron-compose
2504a08..5c2f23f CI -> origin/CI
Checking out 5c2f23f1 as CI...
Skipping Git submodules setup
$ docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.13.1
Storage Driver: overlay
Backing Filesystem: extfs
Supports d_type: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1
runc version: 9df8b306d01f59d3a8029be411de015b7304dd8f
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.8.14-docker-2
Operating System: Alpine Linux v3.5 (containerized)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.751 GiB
Name: 90395a030c02
ID: 7TKR:5PQN:XLFM:EJST:NF2V:NLQC:I2IZ:6OZG:TR4U:ZEAK:EVXE:HIF7
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
$ curl -L "https://github.com/docker/compose/releases/download/1.10.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 600 0 600 0 0 1175 0 --:--:-- --:--:-- --:--:-- 1176
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
3 7929k 3 254k 0 0 116k 0 0:01:08 0:00:02 0:01:06 258k
17 7929k 17 1358k 0 0 433k 0 0:00:18 0:00:03 0:00:15 704k
61 7929k 61 4861k 0 0 1164k 0 0:00:06 0:00:04 0:00:02 1636k
100 7929k 100 7929k 0 0 1703k 0 0:00:04 0:00:04 --:--:-- 2300k
$ chmod +x /usr/local/bin/docker-compose
$ export PATH=/usr/local/bin:$PATH
$ docker-compose build
/bin/sh: eval: line 51: docker-compose: not found
ERROR: Build failed: exit code 127
系统似乎无法找到 docker-compose
,即使安装并添加到路径后也是如此。是否有另一个图像可以制作 docker-compose
命令,或者以不同的方式将其安装在 .gitlab-ci.yml
?
问题
这是个复杂的问题。
docker:latest
图像基于 alpine
(Alpine Linux), which is built using musl-libc
。这个系统非常准系统,因此没有 full-fledged 桌面 Linux可能有。事实上,动态可执行文件需要专门为这个系统编译。
docker-compose
是一个 Python 应用程序,使用 PyInstaller 捆绑到 Linux 可执行文件中。这些可执行文件实际上只能 运行 在它们构建的系统上运行。如果您 运行 一个 alpine
容器,并且 apk add file
,您可以看到您下载的 (dynamically-linked) 可执行文件需要一个特定的解释器。
# file /usr/local/bin/docker-compose
/usr/local/bin/docker-compose.pyinstaller: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=00747fe5bcde089bb4c2f1ba7ab5348bc02ac5bf, stripped
问题是 /lib64/ld-linux-x86-64.so.2
在 alpine
中不存在。事实上,/lib64
根本不存在。
解决方案
因为 docker-compose
是一个 Python 应用程序,它 can also be installed using pip
:
在 alpine
容器内测试:
$ docker run --rm -it alpine /bin/bash
您可以手动 运行 或将这些命令添加到您的 .gitlab-ci.yml
before_script
:
# apk add --no-cache python py2-pip
...
# pip install --no-cache-dir docker-compose
...
# docker-compose -v
docker-compose version 1.11.1, build 7c5d5e4
事实证明实际上有一个未解决的问题:
Docker-compose 现在是 official image。你可以放
image:
name: docker/compose:1.23.2
entrypoint: [""]
在您 .gitlab-ci.yml
的顶部。您需要删除合成图像的默认入口点,这是由第二行完成的。之后,您可以同时使用 docker
和 docker-compose
.
这适用于 GitLab 9.4。您还可以构建自定义图像
FROM docker/compose:1.23.2
ENTRYPOINT []
并将其用作您的图像。
我正在尝试使用以下 .gitlab-ci.yml
文件实施 GitLab 持续集成 (CI) 管道:
image: docker:latest
# When using dind, it's wise to use the overlayfs driver for
# improved performance.
variables:
DOCKER_DRIVER: overlay
services:
- docker:dind
before_script:
- docker info
- curl -L "https://github.com/docker/compose/releases/download/1.10.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
- chmod +x /usr/local/bin/docker-compose
- export PATH=/usr/local/bin:$PATH
- echo $PATH
stages:
- build
- test
build:
stage: build
script:
- docker-compose build
- docker-compose up -d
test:
stage: test
script:
- cd tests/tester
- pytest test_run_tester.py
except:
- /^.*skip_tests$/
但是,在 GitLab 中,我收到以下错误消息:
Running with gitlab-ci-multi-runner 1.10.4 (b32125f)
Using Docker executor with image docker:latest ...
Starting service docker:dind ...
Pulling docker image docker:dind ...
Waiting for services to be up and running...
Pulling docker image docker:latest ...
Running on runner-2e54fd37-project-13-concurrent-0 via scw-de9c9c...
Fetching changes...
HEAD is now at 2504a08 Update CI config
From https://lab.iperlane.com/gio/ipercron-compose
2504a08..5c2f23f CI -> origin/CI
Checking out 5c2f23f1 as CI...
Skipping Git submodules setup
$ docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.13.1
Storage Driver: overlay
Backing Filesystem: extfs
Supports d_type: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1
runc version: 9df8b306d01f59d3a8029be411de015b7304dd8f
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 4.8.14-docker-2
Operating System: Alpine Linux v3.5 (containerized)
OSType: linux
Architecture: x86_64
CPUs: 4
Total Memory: 7.751 GiB
Name: 90395a030c02
ID: 7TKR:5PQN:XLFM:EJST:NF2V:NLQC:I2IZ:6OZG:TR4U:ZEAK:EVXE:HIF7
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Live Restore Enabled: false
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
$ curl -L "https://github.com/docker/compose/releases/download/1.10.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 600 0 600 0 0 1175 0 --:--:-- --:--:-- --:--:-- 1176
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
3 7929k 3 254k 0 0 116k 0 0:01:08 0:00:02 0:01:06 258k
17 7929k 17 1358k 0 0 433k 0 0:00:18 0:00:03 0:00:15 704k
61 7929k 61 4861k 0 0 1164k 0 0:00:06 0:00:04 0:00:02 1636k
100 7929k 100 7929k 0 0 1703k 0 0:00:04 0:00:04 --:--:-- 2300k
$ chmod +x /usr/local/bin/docker-compose
$ export PATH=/usr/local/bin:$PATH
$ docker-compose build
/bin/sh: eval: line 51: docker-compose: not found
ERROR: Build failed: exit code 127
系统似乎无法找到 docker-compose
,即使安装并添加到路径后也是如此。是否有另一个图像可以制作 docker-compose
命令,或者以不同的方式将其安装在 .gitlab-ci.yml
?
问题
这是个复杂的问题。
docker:latest
图像基于 alpine
(Alpine Linux), which is built using musl-libc
。这个系统非常准系统,因此没有 full-fledged 桌面 Linux可能有。事实上,动态可执行文件需要专门为这个系统编译。
docker-compose
是一个 Python 应用程序,使用 PyInstaller 捆绑到 Linux 可执行文件中。这些可执行文件实际上只能 运行 在它们构建的系统上运行。如果您 运行 一个 alpine
容器,并且 apk add file
,您可以看到您下载的 (dynamically-linked) 可执行文件需要一个特定的解释器。
# file /usr/local/bin/docker-compose
/usr/local/bin/docker-compose.pyinstaller: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=00747fe5bcde089bb4c2f1ba7ab5348bc02ac5bf, stripped
问题是 /lib64/ld-linux-x86-64.so.2
在 alpine
中不存在。事实上,/lib64
根本不存在。
解决方案
因为 docker-compose
是一个 Python 应用程序,它 can also be installed using pip
:
在 alpine
容器内测试:
$ docker run --rm -it alpine /bin/bash
您可以手动 运行 或将这些命令添加到您的 .gitlab-ci.yml
before_script
:
# apk add --no-cache python py2-pip
...
# pip install --no-cache-dir docker-compose
...
# docker-compose -v
docker-compose version 1.11.1, build 7c5d5e4
事实证明实际上有一个未解决的问题:
Docker-compose 现在是 official image。你可以放
image:
name: docker/compose:1.23.2
entrypoint: [""]
在您 .gitlab-ci.yml
的顶部。您需要删除合成图像的默认入口点,这是由第二行完成的。之后,您可以同时使用 docker
和 docker-compose
.
这适用于 GitLab 9.4。您还可以构建自定义图像
FROM docker/compose:1.23.2
ENTRYPOINT []
并将其用作您的图像。