作为构建docker-compose文件的一部分运行单元测试后,在容器中创建的文件未显示在我的本地文件系统上。
我有以下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命令后没有任何显示。有什么线索吗?
最佳答案
也许有了这个解释我们就能解决。让我逐步说出一些显而易见的事情,以避免混淆。容器创建有两个步骤:
在第二步中创建卷(容器创建),而命令
dotnet test IDM.Test/IDM.Test.csproj -l "trx;LogFileName=test-results.xml"
在第一个命令中执行(图像创建)。If you creates a folder inside container in the same path where you've mounted volume, data in this new folder will only be available locally inside container.
明确地说,我的建议可以从以下几点恢复:
RUN mkdir /IDM.Test/TestResults/
。 尝试使用此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 - docker-compose不挂载测试结果文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49873949/