【问题标题】:docker-compose not mounting folder for test resultsdocker-compose 没有为测试结果安装文件夹
【发布时间】:2018-04-17 09:02:59
【问题描述】:

在构建 docker-compose 文件的过程中运行 unittests 后,在容器中创建的文件未显示在我的本地文件系统上。

我有以下 Dockerfile:

# IDM.Test/Dockerfile
FROM microsoft/aspnetcore-build:2.0
WORKDIR /src

# Variables
ENV RESTORE_POLICY --no-restore
ENV IGNORE_WARNINGS -nowarn:msb3202,nu1503

# Restore
COPY IDM.sln ./

# Copying and restoring other projects...

COPY IDM.Test/IDM.Test.csproj IDM.Test/
RUN dotnet restore IDM.Test/IDM.Test.csproj $IGNORE_WARNINGS

# Copy
COPY . .

# Test
RUN dotnet test IDM.Test/IDM.Test.csproj -l "trx;LogFileName=test-results.xml"
RUN ls -alR

运行RUN ls -alR 时,我可以看到文件/src/IDM.Test/TestResults/test-results.xml 是在容器内生成的。到目前为止一切顺利。

我正在使用docker-compose -f docker-compose.test.yml build 开始构建。 docker-compose 看起来像这样:

version: '3'

services:
  idm.webapi:
    image: idmwebapi
    build:
      context: .
      dockerfile: IDM.Test/Dockerfile
    volumes:
      - ./IDM.Test/TestResults:/IDM.Test/TestResults/

我已经在本地创建了文件夹IDM.Test/TestResults,但是成功运行docker-compose build命令后什么都没有出现。

有什么线索吗?

【问题讨论】:

  • 你能不能把./IDM.Test/TestResults:/IDM.Test/TestResults/改成./IDM.Test/TestResults:/IDM.Test/TestResults(去掉最后一个斜杠)
  • 试过了,没用
  • 你确定目录/IDM.Test/TestResults在容器的根目录下吗?
  • 在 dockerfile 中,/src 设置为 workdir,在成功构建后,我得到以下输出:Total tests: 183. Passed: 183. Failed: 0. Skipped: 0. Test Run Successful. Test execution time: 23.1050 Seconds Results File: /src/IDM.Test/TestResults/test-results.xml Removing intermediate container d84ea055f2b6 ---> 10e517b0dfcb Successfully built 10e517b0dfcb Successfully tagged idmwebapi:latest 在路径中显示文件:` /src/IDM.Test/TestResults/test-results .xml`
  • 在这种情况下你必须挂载卷./IDM.Test/TestResults:/src/IDM.Test/TestResults

标签: docker docker-compose docker-volume


【解决方案1】:

也许有了这个解释,我们可以解决它。让我一步一步说一些明显的事情以避免混淆。容器创建有两个步骤:

  1. docker build / docker-compose build -> 创建镜像
  2. docker run / docker compose up / docker-compose run -> 创建容器

卷在第二步中创建(容器创建),而您的命令 dotnet test IDM.Test/IDM.Test.csproj -l "trx;LogFileName=test-results.xml" 正在第一个步骤中执行(图像创建)。

如果您在容器内创建的文件夹与您创建的路径相同 挂载的卷,这个新文件夹中的数据将仅在本地可用 容器内。

当然,我的建议可以从以下几点恢复:

  • 检查挂载卷的目标文件夹是否未在构建阶段创建,因此未在 Dockerfile 中定义任何 RUN mkdir /IDM.Test/TestResults/
  • 另一个小建议不是强制性的,但我喜欢在 docker-compose 文件中定义具有绝对路径的卷。
  • 不要在 Dockerfile 中执行在外部生成您想要的数据的命令,除非您将此命令指定为 ENTRYPOINT 或 CMD,而不是 RUN。
  • 在 Dockerfile 中,ENTRYPOINT 或 CMD(或命令:在 docker-compose 中)指定容器启动时在 buildind 之后执行的命令。

试试这个 Dockerfile:

# IDM.Test/Dockerfile
FROM microsoft/aspnetcore-build:2.0
WORKDIR /src

# Variables
ENV RESTORE_POLICY --no-restore
ENV IGNORE_WARNINGS -nowarn:msb3202,nu1503

# Restore
COPY IDM.sln ./

# Copying and restoring other projects...

COPY IDM.Test/IDM.Test.csproj IDM.Test/
RUN dotnet restore IDM.Test/IDM.Test.csproj $IGNORE_WARNINGS

# Copy
COPY . .

# Test
CMD dotnet test IDM.Test/IDM.Test.csproj -l "trx;LogFileName=test-results.xml"

或者这个 docker-compose: 版本:'3'

services:
  idm.webapi:
    image: idmwebapi
    build:
      context: .
      dockerfile: IDM.Test/Dockerfile
    volumes:
      - ./IDM.Test/TestResults:/IDM.Test/TestResults/
    command: >
      dotnet test IDM.Test/IDM.Test.csproj -l "trx;LogFileName=test-results.xml"

容器创建后,您可以通过ls检查您生成的文件。

【讨论】:

  • 谢谢,使用docker-compose up 代替docker-compose build 成功了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-21
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多