我正在構建.NET Core 2.0 Web API,並且正在創建一個Docker鏡像。我對Docker頗爲陌生,所以如果之前已經回答了問題,我很抱歉。從多階段Docker構建(.NET Core 2.0)中提取單元測試結果
我有用於創建圖像的以下Docker文件。特別是,我在構建過程中運行單元測試,並將結果輸出到./test/test_results.xml
(在構建過程中創建的臨時容器中,我猜)。我的問題是,如何在構建完成後訪問這些測試結果?
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"]
我採取的一種方法是在行# COPY --from=build-env /app/test/test_results.xml .
註釋。這使我的最終形象test_results.xml
。然後,我可以使用以下PowerShell腳本提取這些結果並從最終映像中刪除test_results.xml
。
$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
這似乎很難看,我想知道是否有更乾淨的方法來做到這一點。
我希望有一種方法可以在docker build
期間安裝卷,但似乎並沒有在官方Docker中支持。
我現在正在創建一個單獨的圖像,僅用於單元測試。
不知道是否有推薦的方式來實現我想要的。
你有沒有找到一個解決方案呢?我遇到了完全相同的問題。 – Mike
@Mike不,對不起,還沒有。我剛剛採用了我提到的將測試結果臨時添加到最終圖像並提取它們的方法。相當新的Docker,所以我只是想檢查是否有其他方式做這個或推薦的方式。 – Francis