Extract unit test results from multi-stage Docker build (.NET Core 2.0) : <p>I am building a .NET Core 2.0 web API and I am creating a Docker image. I am quite new to Docker so apologies if the question has been answered before.</p>

<p>I have the following Docker file for creating the image. In particular, I run the unit tests during the build process and the results are output to <code>./test/test_results.xml</code> (in a temporary container created during the build, I guess). My question is, how do I access these test results after the build has finished?</p>

<pre><code>FROM microsoft/aspnetcore-build:2.0 AS build-env

WORKDIR /app

# Copy main csproj file for DataService
COPY src/DataService.csproj ./src/
RUN dotnet restore ./src/DataService.csproj

# Copy test csproj file for DataService
COPY test/DataService.Tests.csproj ./test/
RUN dotnet restore ./test/DataService.Tests.csproj

# Copy everything else (excluding elements in dockerignore)
COPY . ./

# Run the unit tests
RUN dotnet test --results-directory ./ --logger "trx;LogFileName=test_results.xml" ./test/DataService.Tests.csproj

# Publish the app to the out directory
RUN dotnet publish ./src/DataService.csproj -c Release -o out

# Build the runtime image
FROM microsoft/aspnetcore:2.0
WORKDIR /app
EXPOSE 5001
COPY --from=build-env /app/src/out .

# Copy test results to the final image as well??
# COPY --from=build-env /app/test/test_results.xml .

ENTRYPOINT ["dotnet", "DataService.dll"]
</code></pre>

<p>One approach that I have taken is to comment in the line <code># COPY --from=build-env /app/test/test_results.xml .</code>. This puts the <code>test_results.xml</code> in my final image. I can then extract these results and remove the <code>test_results.xml</code> from the final image using the following powershell script.</p>

<pre><code>$id=$(docker create dataservice)
docker cp ${id}:app/test_results.xml ./test/test_results.xml
docker start $id
docker exec $id rm -rf /app/test_results.xml
docker commit $id dataservice
docker rm -vf $id
</code></pre>

<p>This however seems ugly and I am wondering is there a cleaner way to do it.</p>

<p>I was hoping that there was a way to mount a volume during <code>docker build</code> but it does not appear that this is going to be supported in the official Docker.</p>

<p>I am looking now at creating a separate image, solely for the unit tests.</p>

<p>Not sure if there is a recommended way of achieving what I want.</p>
