我尝试使用 .net core 6 构建图像,但是当命令运行到这一行时:
RUN dotnet publish -c release -o /app --no-restore
我遇到错误:
#14 0.629 /usr/share/dotnet/sdk/6.0.101/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(267,5):
错误 NETSDK1064:包 Microsoft.EntityFrameworkCore.Analyzers, 未找到版本 6.0.1。它可能自 NuGet 以来已被删除 恢复。否则,NuGet 恢复可能仅部分完成, 这可能是由于最大路径长度限制造成的。 [/source/Dating_WebAPI.csproj]
所以我检查了我的nuget管理器,我之前没有下载这个包。
但即使我下载了,仍然出现错误。
我错过了什么吗?
以下 Dockerfile:
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /source
# copy csproj and restore as distinct layers
COPY *.csproj .
RUN dotnet restore
# copy everything else and build app
COPY . .
RUN dotnet publish -c release -o /app --no-restore
# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=build /app .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
dockerfile步骤:
[+] Building 12.2s (14/15)
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 768B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:latest 0.3s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:latest 0.2s
=> [internal] load build context 0.5s
=> => transferring context: 17.79MB 0.5s
=> [build 1/7] FROM mcr.microsoft.com/dotnet/sdk@sha256:a7af03bdead8976d4e3715452fc985164db56840691941996202cea411953452 0.0s
=> [stage-1 1/3] FROM mcr.microsoft.com/dotnet/aspnet@sha256:7696d5b456eede87434c232b9070f40659ff0c4b71ca622cf197815ccaee661d 0.0s
=> CACHED [stage-1 2/3] WORKDIR /app 0.0s
=> CACHED [build 2/7] WORKDIR /source 0.0s
=> [build 3/7] COPY *.csproj . 0.0s
=> [build 4/7] RUN dotnet clean 0.5s
=> [build 5/7] RUN dotnet restore 10.1s
=> [build 6/7] COPY . . 0.1s
=> ERROR [build 7/7] RUN dotnet publish -c release -o /app --no-restore
从
--no-restore
中删除 dotnet publish
应该可以解决问题。
来自文档https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-publish:
您不必运行 dotnet Restore,因为它由需要进行还原的所有命令隐式运行,例如 dotnet new、dotnet build、dotnet run、dotnet test、dotnetpublish 和 dotnet pack。要禁用隐式恢复,请使用 --no-restore 选项
就我而言,我将
--no-cache /restore
添加到发布阶段。
就像这样:
RUN dotnet publish --no-restore --configuration Release --output /app/publish --no-cache /restore
添加忽略 bin 和 obj 目录的 .dockerignore 文件。
**/bin/
**/obj/