1

我写了一个Dockerfile使用 Docker buildx--mount=type=cache设置来缓存我的 NuGet 包以加快构建速度。正如其他问题所示,这似乎在 .NET 5 中有效。

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS sdk
WORKDIR /src
COPY "ApiTemplate.sln" "."
COPY "Source/ApiTemplate/*.csproj" "Source/ApiTemplate/"
# Run the restore and cache the packages on the host for faster subsequent builds.
RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \
    dotnet restore
COPY . .
RUN dotnet build --configuration Release --no-restore
...

但是,在 .NET 6 中出现以下错误:

 => [linux/amd64 sdk  6/10] RUN --mount=type=cache,id=nuget2,target=/root/.nuget/packages     dotnet restore                                                                                               20.9s
 => CANCELED [linux/arm64 sdk  6/10] RUN --mount=type=cache,id=nuget2,target=/root/.nuget/packages     dotnet restore                                                                                      23.0s
 => [linux/amd64 sdk  7/10] COPY . .                                                                                                                                                                        0.3s
 => ERROR [linux/amd64 sdk  8/10] RUN dotnet build --configuration Release --no-restore                                                                                                                     1.7s
------
 > [linux/amd64 sdk  8/10] RUN dotnet build --configuration Release --no-restore:
#19 0.414 Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
#19 0.414 Copyright (C) Microsoft Corporation. All rights reserved.
#19 0.414
#19 1.321 /usr/share/dotnet/sdk/6.0.100/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1064: Package Microsoft.Extensions.Logging.Abstractions, version 6.0.0 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. [/src/Source/ApiTemplate/ApiTemplate.csproj]
#19 1.698
#19 1.698 Build FAILED.
#19 1.698
#19 1.698 /usr/share/dotnet/sdk/6.0.100/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(267,5): error NETSDK1064: Package Microsoft.Extensions.Logging.Abstractions, version 6.0.0 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. [/src/Source/ApiTemplate/ApiTemplate.csproj]
#19 1.699     0 Warning(s)
#19 1.699     1 Error(s)
#19 1.699
#19 1.699 Time Elapsed 00:00:01.21
------
Dockerfile:52
--------------------
  51 |     COPY . .
  52 | >>> RUN dotnet build --configuration Release --no-restore
  53 |     RUN dotnet test --configuration Release --no-build
  54 |     RUN dotnet publish "Source/ApiTemplate/ApiTemplate.csproj" --configuration Release --no-build --output /app
--------------------
error: failed to solve: process "/bin/sh -c dotnet build --configuration Release --no-restore" did not complete successfully: exit code: 1

似乎dotnet restore失败了。这个问题的解决方案是什么?

4

1 回答 1

1

--mount=type=cache,id=nuget,target=/root/.nuget/packages在构建命令中缺少

于 2022-02-16T21:42:45.393 回答