.net 在多阶段Docker容器中运行c#测试

zpf6vheq  于 2022-12-20  发布在  .NET
关注(0)|答案(1)|浏览(148)

我有以下dockerfile运行我的测试:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
COPY ./ ./
RUN dotnet restore ./mysln.sln -r linux-x64

RUN dotnet build ./tests/mytests/mytests.csproj
ENTRYPOINT ["dotnet", "test", "./tests/mytests/mytests.csproj", "--no-build"]

我希望将构建和测试步骤分开,这样我的映像中就不会有包含obj/bin文件的整个代码库(映像稍后执行,并且可以执行多次,因此没有理由每次都构建它)。
例如:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
COPY ./ ./
RUN dotnet restore ./mysln.sln -r linux-x64

RUN dotnet build ./tests/mytests/mytests.csproj

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS tests

COPY --from=build ./tests/mytests/ ./tests/mytests/

ENTRYPOINT ["dotnet", "test", "./tests/mytests/mytests.csproj", "--no-build"]

然而,由于某种原因,这并不起作用,dotnet测试什么也不做(没有错误报告,没有标准输出)-只是退出,即使它正在目标映像中运行。

oaxa6hgo

oaxa6hgo1#

您的测试阶段是基于sdk:6.0标记的,没有利用您在构建阶段已经完成的复制和构建。
代替:FROM mcr.microsoft.com/dotnet/sdk:6.0 AS tests
使用这个:FROM build AS tests

编辑:还应删除此行:COPY --from=build ./tests/mytests/ ./tests/mytests/

此模式在www.example.com上有文档记录https://github.com/dotnet/dotnet-docker/tree/36e083bb836a5f9a3444ef7ad4459e5c580a7984/samples/complexapp#running-tests-as-an-opt-in-stage

相关问题