Asp.net 核心多项目未能构建默认 Dockerfile

Asp.net core multi project failed to build default Dockerfile

构建时Visual Studio生成的Dockerfile中的RUN dotnet build有错误。这是生成的 Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["API/API.csproj", "API/"]
COPY ["Service/Service.csproj", "Service/"]
COPY ["Extentions/Extensions.csproj", "Extentions/"]
COPY ["DTO/DTO.csproj", "DTO/"]
COPY ["Core/Core.csproj", "Core/"]
COPY ["Data/Data.csproj", "Data/"]
RUN dotnet restore "API/API.csproj"
COPY . .
WORKDIR "/src/API"
RUN dotnet build "API.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "API.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "API.dll"]

这是我的解决方案的结构。

我发现要构建 Dockerfile,我必须像这样从解决方案的根调用它:

docker build -t mysolution:tag -f api/Dockerfile .

但 Dockerfile 的以下行中只有一个错误:

RUN dotnet build "API.csproj" -c Release -o /app/build

我试图将“API.csproj”更改为其他内容,例如“/API/API.csproj”,甚至忽略它,但我在该特定行再次遇到错误。

最后是整个错误:

    [+] Building 8.9s (20/22)
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 876B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 35B                                                                                   0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/sdk:5.0                                                  3.1s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:5.0                                               0.0s
 => [base 1/2] FROM mcr.microsoft.com/dotnet/aspnet:5.0                                                            0.0s
 => [internal] load build context                                                                                  0.2s
 => => transferring context: 111.97kB                                                                              0.2s
 => [build  1/12] FROM mcr.microsoft.com/dotnet/sdk:5.0@sha256:f096bacde1255be2a7b5dad325167f1e76f799b7ee5ee4f5b6  0.0s
 => CACHED [base 2/2] WORKDIR /app                                                                                 0.0s
 => CACHED [final 1/2] WORKDIR /app                                                                                0.0s
 => CACHED [build  2/12] WORKDIR /src                                                                              0.0s
 => CACHED [build  3/12] COPY [API/API.csproj, API/]                                                               0.0s
 => CACHED [build  4/12] COPY [Service/Service.csproj, Service/]                                                   0.0s
 => CACHED [build  5/12] COPY [Extentions/Extensions.csproj, Extentions/]                                          0.0s
 => CACHED [build  6/12] COPY [DTO/DTO.csproj, DTO/]                                                               0.0s
 => CACHED [build  7/12] COPY [Core/Core.csproj, Core/]                                                            0.0s
 => CACHED [build  8/12] COPY [Data/Data.csproj, Data/]                                                            0.0s
 => CACHED [build  9/12] RUN dotnet restore "API/API.csproj"                                                       0.0s
 => CACHED [build 10/12] COPY . .                                                                                  0.0s
 => CACHED [build 11/12] WORKDIR /src/API                                                                          0.0s
 =>ERROR [build 12/12] RUN dotnet build "API.csproj" -c Release -o /app/build                                     5.5s
------
 > [build 12/12] RUN dotnet build "API.csproj" -c Release -o /app/build:
#20 0.814 Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET
#20 0.814 Copyright (C) Microsoft Corporation. All rights reserved.
#20 0.814
#20 1.233   Determining projects to restore...
#20 2.568   All projects are up-to-date for restore.
#20 4.241   DTO -> /app/build/DTO.dll
#20 4.506   Extensions -> /app/build/Extensions.dll
#20 4.556   Core -> /app/build/Core.dll
#20 4.965   Service -> /app/build/Service.dll
#20 5.228   Data -> /app/build/Data.dll
#20 5.480 /src/API/Contracts/V1.cs(12,42): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. [/src/API/API.csproj]
#20 5.480 /src/API/Contracts/V1.cs(13,39): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. [/src/API/API.csproj]
#20 5.484
#20 5.484 Build FAILED.
#20 5.485
#20 5.485 /src/API/Contracts/V1.cs(12,42): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. [/src/API/API.csproj]
#20 5.485 /src/API/Contracts/V1.cs(13,39): error CS8652: The feature 'constant interpolated strings' is currently in Preview and *unsupported*. To use Preview features, use the 'preview' language version. [/src/API/API.csproj]
#20 5.485     0 Warning(s)
#20 5.485     2 Error(s)
#20 5.485
#20 5.485 Time Elapsed 00:00:04.62
------
executor failed running [/bin/sh -c dotnet build "API.csproj" -c Release -o /app/build]: exit code: 1

Constant interpolated strings 是 c# 10 的一部分,因此它与 .net 6 绑定。 您需要为您的 dockerfile 使用合适的图像。

对基础使用 mcr.microsoft.com/dotnet/aspnet:6.0,对构建使用 mcr.microsoft.com/dotnet/sdk:6.0。