使用 Dockerfile 和 wait 执行两个命令



我有两个命令要在Dockerfile中运行。 一个用于运行测试和生成日志。 第二个用于在执行测试后生成 html 报告。 我的 Dockerfile 看起来像这样:

FROM golang:1.13   
ADD . /app
WORKDIR /app
RUN go mod download
RUN go get -u "github.com/ains/go-test-html"
CMD ["make", "test", "$URL=", "$INTEGRATION=", "$TESTTYPE=", "$TAGS="]
CMD ["make", "html", "$HTML="]

我的docker-compose.yml看起来像这样:

version: '3'
services:
tests:
image: int-testing-framework:latest
environment:
- URL=http://localhost:3000/
- INTEGRATION=kapten
- TESTTYPE=contract
- TAGS=quotes bookings getTrip cancelTrip
html:
image: int-testing-framework:latest
command: make html
environment:
- HTML=html
links:
- tests
depends_on:
- tests  

我的日志看起来像这样:

sudo docker-compose up
Creating network "integration_default" with the default driver
Starting integration_tests_1  ... done
Creating integration_html_1 ... done
Attaching to integration_tests_1, integration_html_1
html_1   | Generating HTML report
html_1   | go-test-html logs/[gotest_stdout_file] logs/[gotest_stderr_file] logs/output_file.html
html_1   | Test results written to '/app/logs/output_file.html'
integration_html_1 exited with code 0
tests_1  | Generating HTML report
tests_1  | go-test- logs/[gotest_stdout_file] logs/[gotest_stderr_file] logs/output_file.html
tests_1  | /bin/bash: go-test-: command not found
tests_1  | make: *** [Makefile:14: html] Error 127
integration_tests_1 exited with code 2

它没有完全执行tests:服务。应该有测试日志。关于如何先执行tests:并生成日志的任何想法。然后生成 html 报告?

为此,您只需要一个容器。 使其主命令是一个 shell 脚本,该脚本首先运行测试,然后生成 HTML 报告。

#!/bin/sh
make test
RC=$?
make html
exit "$RC"
CMD ["./run_tests_and_report.sh"]

您也可以通过同时调用两个 Makefile 目标来执行类似操作

CMD ["make", "test", "html"]

(尽管如果测试报告非零退出代码,则不会生成报告(。

在您目前的方法中,有两个问题。 第一个是 Docker 容器只有一个入口点和一个命令,因此您的示例 Dockerfile 有两个CMD行,第二个是生效的行,两个容器都在make html运行。 第二个是 Docker Compose 几乎没有同步选项,特别是没有办法让报告生成等待测试执行完成(除非您以某种方式将其写入容器中的脚本(。

最新更新