简体   繁体   English

Docker 作为 CI 管道组合

[英]Docker Compose as a CI pipeline

So we use Gitlab CI.所以我们使用 Gitlab CI。 The issue was the pain of having to commit each time we want to test wether or not our build pipeline was configured correctly.问题是每次我们想要测试我们的构建管道是否配置正确时都必须提交的痛苦。 Unfortunately no way to easily test Gitlab CI locally when our containers/pipeline ain't workin right.不幸的是,当我们的容器/管道无法正常工作时,无法在本地轻松测试 Gitlab CI。

Our solution, use docker-compose.yml as a CI pipeline runner for local testing of containerized build steps, why not ya know.我们的解决方案,使用docker-compose.yml作为 CI 管道运行器,用于本地测试容器化构建步骤,你为什么不知道。 . . . . ? Basically Gitlab CI, and most others, have each section spawn a container to run a command and won't continue until the preceding steps complete, ie the first step must fully complete and then the next step happens.基本上 Gitlab CI 和大多数其他部分让每个部分生成一个容器来运行命令,并且在前面的步骤完成之前不会继续,即第一步必须完全完成,然后下一步才会发生。

Here is a simple .gitlab-ci.yml file we use:这是我们使用的一个简单.gitlab-ci.yml文件:

stages:
  - install
  - test

cache:
  untracked: true
  key: "$CI_COMMIT_REF_SLUG"
  paths:
    - node_modules/

install:
  image: node:10.15.3
  stage: install
  script: npm install

test:
  image: node:10.15.3
  stage: test
  script: 
    - npm run test
  dependencies:
    - install

Here is the docker-compose.yml file we converted it to:这是我们将其转换为的docker-compose.yml文件:

version: "3.7"
services:
  install:
    image: node:10.15.3
    working_dir: /home/node
    user: node
    entrypoint: npm
    command:
      - install
    volumes:
      - .:/home/node:Z
  test:
    image: node:10.15.3
    working_dir: /home/node
    user: node
    entrypoint: npm
    command:
      - run
      - test
    volumes:
      - .:/home/node:Z
    depends_on:
      - install

OK, now for the real issue here.好的,现在是真正的问题。 The depends_on part of the compose file doesn't wait for the install container to finish, it just waits for the npm command to be running. compose 文件的depends_on部分不等待install容器完成,它只是等待 npm 命令运行。 Therefore, once the npm command is officially loaded up and running, the test container will start running and complain there are no node_modules yet.因此,一旦 npm 命令正式加载并运行, test容器将开始运行并抱怨还没有node_modules This happens because npm is running does not mean the npm command has actually finished.发生这种情况是因为 npm 正在运行并不意味着 npm 命令实际上已经完成。

Anyone know any tricks to better control what docker considers to be done .任何人都知道更好地控制 docker 认为要done的事情的任何技巧。 All the solutions I looked into where using some kind of wrapper script which watched some port on the internal docker.network to wait for a service, like a db, to be fully turned on and ready.我研究的所有解决方案都使用某种包装脚本,该脚本监视内部 docker.network 上的某个端口以等待服务(如数据库)完全打开并准备就绪。

When using k8s I can setup a readiness probe which is super dope, doesn't seem to be a feature of Docker Compose though.使用 k8s 时,我可以设置一个准备就绪探测器,它非常棒,但似乎不是 Docker Compose 的功能。 Am I wrong here?我在这里错了吗? Would be nice to just write a command which docker uses to determine what done means.最好只写一个命令,docker 使用它来确定done的含义。

For now we must run each step manually and then run the next when the preceding step is complete like so:现在我们必须手动运行每个步骤,然后在前面的步骤完成后运行下一步,如下所示:

docker-compose up install

wait....等待....

docker-compose up test

We really just want to say:我们真的只想说:

docker-compose up

and have all the steps complete in correct order by waiting for preceding steps.并通过等待前面的步骤以正确的顺序完成所有步骤。

I went through the same issue, this is a permission related thing when you are mapping from your local machine to docker.我遇到了同样的问题,当您从本地计算机映射到 docker 时,这是与权限相关的事情。

volumes:
  - .:/home/node:Z

Create a file inside the container, and check the permission of this same file in your local machine, if you see the root user or anything else is the owner, instead of your current user, you have to run first在容器内创建一个文件,并在本地机器上检查同一个文件的权限,如果你看到 root 用户或其他任何东西是所有者,而不是你当前的用户,你必须先运行

export DOCKER_USER="$(id -u):$(id -g)"

and change并改变

user: node

by经过

user: $DOCKER_USER

PS: I'm assuming you can run docker without having to use sudo, just mentioning this bc this is the scenario I have. PS:我假设你可以运行 docker 而不必使用 sudo,只是提到这个 bc 这是我的场景。

This question was many years ago.这个问题是很多年前的了。 I now use this project: https://github.com/firecow/gitlab-ci-local我现在使用这个项目: https://github.com/firecow/gitlab-ci-local

It runs your Gitlab Pipeline locally using docker just as you would expect it to run.它使用 docker 在本地运行 Gitlab 管道,就像您期望的那样。

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

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