测试容器完成时终止 docker compose [英] Terminate docker compose when test container finishes

查看:12
本文介绍了测试容器完成时终止 docker compose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在运行一个 docker-compose 堆栈,用于使用量角器测试运行器、一个为网页提供服务的 nodejs 服务器和一个为 java 后端提供服务的 Wildfly 服务器进行基本集成测试.

I am currently running a docker-compose stack for basic integration tests with a protractor test runner, a nodejs server serving a web page and a wildfly server serving a java backend.

堆栈从我的构建服务器(concourse ci)中的 dind(docker in docker)容器运行.

The stack is run from a dind(docker in docker) container in my build server(concourse ci).

但看起来容器并没有在完​​成量角器测试时终止.

But it appears that the containers does not terminate on finishing the protractor tests.

因此,由于 wildfly 和 nodejs 的容器仍在运行,因此构建任务永远不会完成......

如何在测试完成后让撰写以成功或失败告终?

# Test runner
test-runner:
  image: "${RUNNER_IMG}"
  privileged: true
  links:
    - client
    - server
  volumes:
  - /Users/me/frontend_test/client-devops:/protractor/project
  - /dev/shm:/dev/shm
  entrypoint:
    - /entrypoint.sh
    - --baseUrl=http://client:9000/dist/
    - /protractor/conf-dev.js
    - --suite=remember
# Client deployment
client:
  image: "${CLIENT_IMG}"
  links:
    - server
# Server deployment
server:
  image: "${SERVER_IMG}"

推荐答案

Compose 已将 --exit-code-from {container} 标志添加到 docker-compose up 这使得这更容易.

Compose has added the --exit-code-from {container} flag to docker-compose up which makes this easier.

docker-compose up --exit-code-from test-runner

请参阅 Michael Spector 的回答了解更多详情.

See Michael Spector's answer for more detail.

与此rspec q/a类似,您需要将测试作为报告退出状态的独立任务运行回到你的 CI.

Similar to this rspec q/a, you need to run the tests as a standalone task that report an exit status back to your CI.

您可以将测试运行器分离到它自己的 yaml 中,或者将测试运行器修改为默认为无操作命令/入口点.

You could separate the test-runner into it's own yaml or modify the test-runner to default to a no op command/entrypoint.

单独指定 test-runner 配置(您可能需要升级到 版本 2 networks 而不是使用 links 来处理多个撰写文件).

Specify the test-runner config separately (You might need to upgrade to version 2 networks instead of using links to work across multiple compose files).

docker-compose up -d
docker-compose -f test-runner.yml run test-runner
rc=$?
docker-compose down
exit $rc

没有 op 测试运行器

默认 test-runner 为 no op 入口点/命令,然后手动运行测试命令

No op test runner

Default the test-runner to a no op entrypoint/command and then manually run the test command

services:
  test-runner:
    image: "${RUNNER_IMG}"
    command: 'true'

然后

docker-compose up -d
docker-compose run test-runner /launch-tests.sh
rc=$?
docker-compose down
exit $rc

返回代码

如果您的 CI 有发布任务"的概念您也许可以跳过 rc 捕获并在 test-runner CI 任务完成后运行 docker-compose down.您的 CI 也可能为您清理容器.

Return codes

If your CI has the concept of "post tasks" you might be able to skip the rc capturing and just run the docker-compose down after the test-runner CI task has completed. It's also possible your CI cleans up the containers for you.

这篇关于测试容器完成时终止 docker compose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆