Docker Buildkit --mount=type=cache 用于缓存 .NET 6 的 Nuget 包

Docker Buildkit --mount=type=cache for Caching Nuget Packages for .NET 6

我写了一个 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 失败了。这个问题的解决方案是什么?

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