Docketfile - 单元测试不适用于多个构建目标
Docketfile - Unit test doesn't work with multiple build target
我有以下 docker 文件,其中有多个构建目标,包括 .net 单元测试。
出于某种原因,它在构建过程中没有通过单元测试,但如果我评论测试运行ner 并测试构建目标点,它就可以工作。
有什么想法吗?
我的文件夹结构:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/runtime:3.1 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY *.sln .
COPY ["Pokemon.API/*.csproj", "./Pokemon.API/"]
COPY ["Pokemon.Core/*.csproj", "./Pokemon.Core/"]
COPY ["Pokemon.Models/*.csproj", "./Pokemon.Models/"]
COPY ["Pokemon.Test/*.csproj", "./Pokemon.Test/"]
RUN dotnet restore
# copy full solution over
COPY . .
RUN dotnet build
# create a new build target called testrunner
FROM build AS testrunner
WORKDIR /src/Pokemon.Test
CMD ["dotnet", "test", "--logger:trx"]
# run the unit tests
FROM build AS test
WORKDIR /src/Pokemon.Test
RUN dotnet test --logger:trx
FROM build AS release
WORKDIR "/src/Pokemon.API"
RUN dotnet build "Pokemon.API.csproj" -c Release -o /app/build
FROM build AS publish
WORKDIR "/src/Pokemon.API"
RUN dotnet publish "Pokemon.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Pokemon.API.dll"]
结果输出:
如果我在 Docker 文件的下方注释行,它成功 运行 构建期间的单元测试
# create a new build target called testrunner
#FROM build AS testrunner
#WORKDIR /src/Pokemon.Test
#CMD ["dotnet", "test", "--logger:trx"]
# run the unit tests
#FROM build AS test
#WORKDIR /src/Pokemon.Test
RUN dotnet test --logger:trx
您在执行 docker build
时没有指定目标构建阶段。因此,默认情况下,它以要构建的最后阶段为目标。但是最后阶段不依赖于 test运行ner 或测试阶段,因此它们都不会被构建。如果您想明确 运行 一个测试阶段,您可以指定 --target
选项以针对特定阶段。例如:docker build -t pokemonapi:v1 -f Dockerfile --target test .
我有以下 docker 文件,其中有多个构建目标,包括 .net 单元测试。
出于某种原因,它在构建过程中没有通过单元测试,但如果我评论测试运行ner 并测试构建目标点,它就可以工作。
有什么想法吗?
我的文件夹结构:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/core/runtime:3.1 AS base
WORKDIR /app
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build
WORKDIR /src
COPY *.sln .
COPY ["Pokemon.API/*.csproj", "./Pokemon.API/"]
COPY ["Pokemon.Core/*.csproj", "./Pokemon.Core/"]
COPY ["Pokemon.Models/*.csproj", "./Pokemon.Models/"]
COPY ["Pokemon.Test/*.csproj", "./Pokemon.Test/"]
RUN dotnet restore
# copy full solution over
COPY . .
RUN dotnet build
# create a new build target called testrunner
FROM build AS testrunner
WORKDIR /src/Pokemon.Test
CMD ["dotnet", "test", "--logger:trx"]
# run the unit tests
FROM build AS test
WORKDIR /src/Pokemon.Test
RUN dotnet test --logger:trx
FROM build AS release
WORKDIR "/src/Pokemon.API"
RUN dotnet build "Pokemon.API.csproj" -c Release -o /app/build
FROM build AS publish
WORKDIR "/src/Pokemon.API"
RUN dotnet publish "Pokemon.API.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Pokemon.API.dll"]
结果输出:
如果我在 Docker 文件的下方注释行,它成功 运行 构建期间的单元测试
# create a new build target called testrunner
#FROM build AS testrunner
#WORKDIR /src/Pokemon.Test
#CMD ["dotnet", "test", "--logger:trx"]
# run the unit tests
#FROM build AS test
#WORKDIR /src/Pokemon.Test
RUN dotnet test --logger:trx
您在执行 docker build
时没有指定目标构建阶段。因此,默认情况下,它以要构建的最后阶段为目标。但是最后阶段不依赖于 test运行ner 或测试阶段,因此它们都不会被构建。如果您想明确 运行 一个测试阶段,您可以指定 --target
选项以针对特定阶段。例如:docker build -t pokemonapi:v1 -f Dockerfile --target test .