简体   繁体   English

在 .gitlab-ci.yml 中运行 docker-compose build

[英]Run docker-compose build in .gitlab-ci.yml

I have a .gitlab-ci.yml file which contains following:我有一个.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

But in ci-log I receive message:但在ci-log我收到消息:

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

What am I doing wrong?我做错了什么?

Following the official documentation :按照官方文档

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

Sample docker-compose.yml:示例 docker-compose.yml:

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

We personally do not follow this flow anymore, because you loose control about the running containers and they might end up running endless.我们个人不再遵循此流程,因为您对正在运行的容器失去了控制,它们最终可能会无休止地运行。 This is because of the docker-in-docker executor.这是因为 docker-in-docker 执行程序。 We developed a python-script as a workaround to kill all old containers in our CI, which can be found here .我们开发了一个 python 脚本作为一种解决方法来杀死我们 CI 中的所有旧容器,可以在这里找到 But I do not suggest to start containers like this anymore.但我不建议再启动这样的容器。

Docker also provides an official image: docker/compose Docker 还提供了一个官方镜像: docker/compose

This is the ideal solution if you don't want to install it every pipeline.如果您不想在每个管道中安装它,这是理想的解决方案。

Note that in the latest version of GitLab CI/Docker you will likely need to give privileged access to your GitLab CI Runner and configure/disable TLS.请注意,在最新版本的 GitLab CI/Docker 中,您可能需要授予对 GitLab CI Runner 的特权访问权限并配置/禁用 TLS。 See Use docker-in-docker workflow with Docker executor请参阅将 docker-in-docker 工作流与 Docker 执行程序一起使用

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

Note that in versions of docker-compose earlier than 1.25 :请注意,在docker-compose 早于 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 .由于图像使用docker-compose-entrypoint.sh作为入口点,因此您需要将其覆盖.gitlab-ci.yml /bin/sh -c Otherwise your pipeline will fail with No such command: sh否则你的管道将失败, No such command: sh

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

I created a simple docker container which has docker-compose installed on top of docker:latest .我创建了一个简单的 docker 容器,它在docker:latest之上安装了docker-compose See https://hub.docker.com/r/tmaier/docker-compose/https://hub.docker.com/r/tmaier/docker-compose/

Your .gitlab-ci.yml file would look like this:您的.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

EDIT I added another answer providing a minimal example for a .gitlab-ci.yml configuration supporting docker-compose.编辑我添加了另一个答案,为支持 docker-compose 的 .gitlab-ci.yml 配置提供了一个最小的例子。


docker-compose can be installed as a Python package, which is not shipped with your image. docker-compose可以作为 Python 包安装,它不随您的图像一起提供。 The image you chose does not even provide an installation of Python:您选择的图像甚至不提供 Python 的安装:

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

Looking for Python gives an empty result.寻找 Python 给出了一个空的结果。 So you have to choose a different image, which fits to your needs and ideally has docker-compose installed or you maually create one.所以你必须选择一个不同的图像,它适合你的需要,最好安装 docker-compose 或者你手动创建一个。

The docker image you chose uses Alpine Linux.您选择的 docker 镜像使用 Alpine Linux。 You can use it as a base for your own image or try a different one first if you are not familiar with Alpine Linux.如果您不熟悉 Alpine Linux,您可以将其用作您自己映像的基础,或者先尝试不同的映像。

I had the same issue and created a Dockerfile in a public GitHub repository and connected it with my Docker Hub account and chose an automated build to build my image on each push to the GitHub repository.我遇到了同样的问题,在公共 GitHub 存储库中创建了一个 Dockerfile 并将其与我的 Docker Hub 帐户连接,并选择了一个自动构建来在每次推送到 GitHub 存储库时构建我的映像。 Then you can easily access your own images with the GitLab CI.然后您可以使用 GitLab CI 轻松访问您自己的图像。

If you don't want to provide a custom docker image with docker-compose preinstalled, you can get it working by installing Python during build time.如果您不想提供预装了 docker-compose 的自定义 docker 映像,您可以通过在构建期间安装 Python 来使其工作。 With Python installed you can finally install docker-compose ready for spinning up your containers.安装 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/

Use docker-compose exec with -T if you receive this or a similar error:如果您收到此错误或类似错误,请将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

I think most of the above are helpful, however i needed to collectively apply them to solve this problem, below is the script which worked for me我认为以上大部分内容都有帮助,但是我需要共同应用它们来解决这个问题,下面是对我有用的脚本

I hope it works for you too我希望它也适用于你

Also note, in your docker compose this is the format you have to provide for the image name另请注意,在您的 docker compose 中,这是您必须为图像名称提供的格式

<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

It really took me some time to get it working with Gitlab.com shared runners.我真的花了一些时间才让它与 Gitlab.com 共享跑步者一起工作。

I'd like to say "use docker/compose:latest and that's it", but unfortunately I was not able to make it working, I was getting Cannot connect to the Docker daemon at tcp://docker:2375/. Is the docker daemon running?我想说“使用docker/compose:latest就是这样”,但不幸的是我无法让它工作,我Cannot connect to the Docker daemon at tcp://docker:2375/. Is the docker daemon running?遇到了Cannot connect to the Docker daemon at tcp://docker:2375/. Is the docker daemon running? Cannot connect to the Docker daemon at tcp://docker:2375/. Is the docker daemon running? error even when all the env variables were set.即使设置了所有 env 变量也会出错。

Neither I like an option to install five thousands of dependencies to install docker-compose via pip.我也不喜欢安装五千个依赖项以通过 pip 安装docker-compose的选项。

Fortunately, for the recent Alpine versions (3.10+) there is docker-compose package in Alpine repository.幸运的是,对于最近的 Alpine 版本(3.10+),Alpine 存储库中有 docker -compose包。 It means that @n2o's answer can be simplified to:这意味着@n2o 的答案可以简化为:

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

This worked perfectly from the first try for me.这对我来说从第一次尝试就非常有效。 Maybe the reason other answers didn't was in some configuration of Gitlab.com shared runners, I don't know...也许其他答案没有的原因是 Gitlab.com 共享跑步者的某些配置,我不知道......

there is tiangolo/docker-with-compose which works: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 ...

Alpine linux now has a docker-compose package in their "edge" branch, so you can install it this way in .gitlab-ci.yml Alpine linux 现在在其“edge”分支中有一个 docker-compose 包,因此您可以在 .gitlab-ci.yml 中以这种方式安装它


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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM