gpt4 book ai didi

docker - 使用Dockerfile执行两个命令

转载 作者:行者123 更新时间:2023-12-01 22:34:52 25 4
gpt4 key购买 nike

我有两个要在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几乎没有同步选项,尤其是没有办法使报告生成等待测试执行完成(除非您以某种方式将其写到了容器的脚本中)。

关于docker - 使用Dockerfile执行两个命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59946712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com