运行 docker-在 .gitlab 中构建构建-ci.yml

Run docker-compose build in .gitlab-ci.yml

我有一个 .gitlab-ci.yml 文件,其中包含以下内容:

image: docker:latest

services:
  - docker:dind

before_script:
  - docker info
  - docker-compose --version

buildJob:
  stage: build
  tags:
    - docker
  script:
    - docker-compose build

但是在 ci-log 我收到消息:

$ docker-compose --version
/bin/sh: eval: line 46: docker-compose: not found

我做错了什么?

EDIT 我添加了另一个答案,为支持 docker-compose.

的 .gitlab-ci.yml 配置提供了一个最小示例

docker-compose 可以作为 Python 包安装,它不随您的图像一起提供。您选择的图像甚至不提供 Python:

的安装
$ docker run --rm -it docker sh
/ # find / -iname "python"
/ # 

查找 Python 得到空结果。所以你必须选择一个不同的图像,它适合你的需要,最好安装 docker-compose 或者你手动创建一个。

您选择的 docker 图像使用 Alpine Linux。您可以将它用作您自己图像的基础,或者如果您不熟悉 Alpine Linux.

,请先尝试不同的图像

我遇到了同样的问题并在 public GitHub 存储库中创建了一个 Docker 文件并将其与我的 Docker Hub 帐户连接并选择了自动构建以在每次推送到 GitHub 存储库时构建我的图像。然后,您可以使用 GitLab CI.

轻松访问自己的图像

如果您不想提供预装了 docker-compose 的自定义 docker 图像,您可以通过在构建期间安装 Python 来使其正常工作。安装 Python 后,您终于可以安装 docker-compose 准备启动您的容器了。

image: docker:latest

services:
- docker:dind

before_script:
- apk add --update python py-pip python-dev && pip install docker-compose # install docker-compose
- docker version
- docker-compose version

test:
  cache:
    paths:
    - vendor/
  script:
  - docker-compose up -d
  - docker-compose exec -T php-fpm composer install --prefer-dist
  - docker-compose exec -T php-fpm vendor/bin/phpunit --coverage-text --colors=never --whitelist src/ tests/

如果收到此错误或类似错误,请使用 docker-compose exec-T

$ docker-compose exec php-fpm composer install --prefer-dist
Traceback (most recent call last):
  File "/usr/bin/docker-compose", line 9, in <module>
    load_entry_point('docker-compose==1.8.1', 'console_scripts', 'docker-compose')()
  File "/usr/lib/python2.7/site-packages/compose/cli/main.py", line 62, in main
    command()
  File "/usr/lib/python2.7/site-packages/compose/cli/main.py", line 114, in perform_command
    handler(command, command_options)
  File "/usr/lib/python2.7/site-packages/compose/cli/main.py", line 442, in exec_command
    pty.start()
  File "/usr/lib/python2.7/site-packages/dockerpty/pty.py", line 338, in start
    io.set_blocking(pump, flag)
  File "/usr/lib/python2.7/site-packages/dockerpty/io.py", line 32, in set_blocking
    old_flag = fcntl.fcntl(fd, fcntl.F_GETFL)
ValueError: file descriptor cannot be a negative integer (-1)
ERROR: Build failed: exit code 1

我创建了一个简单的 docker 容器,其中 docker-compose 安装在 docker:latest 之上。参见 https://hub.docker.com/r/tmaier/docker-compose/

您的 .gitlab-ci.yml 文件将如下所示:

image: tmaier/docker-compose:latest

services:
  - docker:dind

before_script:
  - docker info
  - docker-compose --version

buildJob:
  stage: build
  tags:
    - docker
  script:
    - docker-compose build

official documentation之后:

# .gitlab-ci.yml
image: docker
services:
  - docker:dind    
build:
  script:
    - apk add --no-cache docker-compose
    - docker-compose up -d

示例 docker-compose.yml:

version: "3.7"
services:
  foo:
    image: alpine
    command: sleep 3
  bar:
    image: alpine
    command: sleep 3

我们个人不再遵循此流程,因为您失去了对 运行 容器的控制,它们最终可能会 运行 无穷无尽。这是因为 docker-in-docker 执行器。我们开发了一个 python 脚本作为变通方法来杀死 CI 中的所有旧容器,其中 can be found here。但是我不建议再这样启动容器了。

Docker 还提供了一张 官方 图片:docker/compose

如果您不想在每个管道都安装它,这是理想的解决方案。

请注意,在最新版本的 GitLab CI/Docker 中,您可能需要授予对 GitLab CI Runner 和 configure/disable TLS 的特权访问权限。参见 Use docker-in-docker workflow with Docker executor

variables:
  DOCKER_HOST: tcp://docker:2375/
  DOCKER_DRIVER: overlay2

# Official docker compose image.
image:
  name: docker/compose:latest

services:
  - docker:dind

before_script:
  - docker version
  - docker-compose version

build:
  stage: build
  script:
    - docker-compose down
    - docker-compose build
    - docker-compose up tester-image

请注意,在 docker-compose earlier than 1.25 的版本中:

Since the image uses docker-compose-entrypoint.sh as entrypoint you'll need to override it back to /bin/sh -c in your .gitlab-ci.yml. Otherwise your pipeline will fail with No such command: sh

    image:
      name: docker/compose:latest
      entrypoint: ["/bin/sh", "-c"]

Alpine linux 现在在他们的 "edge" 分支中有一个 docker-compose 包,所以你可以这样在 .gitlab-ci.yml[=11 中安装它=]


a-job-with-docker-compose:
  image: docker
  services:
    - docker:dind
  script:
    - apk add docker-compose --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted
    - docker-compose -v

我认为上面的大部分内容都有帮助,但是我需要将它们集中起来解决这个问题,下面是对我有用的脚本

希望对你也有用

另请注意,在您的 docker 撰写中,这是您必须为图像名称提供的格式

<registry base url>/<username>/<repo name>/<image name>:<tag>

image:
  name: docker/compose:latest
  entrypoint: ["/bin/sh", "-c"]

variables:
  DOCKER_HOST: tcp://docker:2375/
  DOCKER_DRIVER: overlay2

services:
  - docker:dind

stages:
- build_images

before_script:
  - docker version
  - docker-compose version
  - docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN $CI_REGISTRY

build:
  stage: build_images
  script:
    - docker-compose down
    - docker-compose build
    - docker-compose push

我真的花了一些时间才让它与 Gitlab.com 共享跑步者一起工作。

我想说 "use docker/compose:latest and that's it",但不幸的是我无法让它工作,即使设置了所有 env 变量,我仍然收到 Cannot connect to the Docker daemon at tcp://docker:2375/. Is the docker daemon running? 错误。

我也不喜欢通过 pip 安装五千个依赖项的选项docker-compose

幸运的是,对于最近的 Alpine 版本 (3.10+),docker-compose package in Alpine repository. It means that @n2o's 可以简化为:

test:
  image: docker:19.03.0

  variables:
    DOCKER_DRIVER: overlay2
    # Create the certificates inside this directory for both the server
    # and client. The certificates used by the client will be created in
    # /certs/client so we only need to share this directory with the
    # volume mount in `config.toml`.
    DOCKER_TLS_CERTDIR: "/certs"

  services:
    - docker:19.03.0-dind

  before_script:
    - apk --no-cache add docker-compose      # <---------- Mind this line
    - docker info
    - docker-compose --version

  stage: test
  script:
      - docker-compose build

这对我来说从第一次尝试开始就非常有效。也许其他答案没有的原因是 Gitlab.com 共享跑步者的某些配置,我不知道...

tiangolo/docker-with-compose 有效:

image: tiangolo/docker-with-compose

stages:
  - build
  - test
  - release
  - clean

  
before_script:
  - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com

build:
  stage: build
  script:
    - docker-compose -f docker-compose-ci.yml build --pull 


test1:
    stage: test
    script:
        - docker-compose -f docker-compose-ci.yml up -d
        - docker-compose -f docker-compose-ci.yml exec -T php ...