docker - 使用Dockerfile执行两个命令

标签 docker go docker-compose dockerfile docker-command

我有两个要在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/

相关文章:

json - 将 `struct` 作为类型名称传递给函数参数

docker - 如何在具有共享卷的 2 个容器之间更新文件

docker - 如何为多个 tar 文件做 "docker load"

python - 如何将 python 命令传递给 dockerfile

docker - 如何在 Jenkins Docker 的目录名称中使用带有重音符号的 Maven 程序集?

go - Go 中方法的作用域是什么?

http - 为 golang 请求设置标志

docker - 如何检查 docker 镜像依赖于哪个操作系统?

docker - dotnet core 2.2 webAPI 在 Docker 中容器化后没有响应

php - AH01276 : Cannot serve directory/var/www/html/: No matching DirectoryIndex (index. php,index.html) 找到