【问题标题】:Docker Buildkit --mount=type=cache for Caching Nuget Packages for .NET 6Docker Buildkit --mount=type=cache 用于缓存 .NET 6 的 Nuget 包
【发布时间】:2022-01-07 21:45:45
【问题描述】:

我写了一个Dockerfile,它使用 Docker buildx --mount=type=cache 设置来缓存我的 NuGet 包以加快构建速度。如other question 所示,这似乎在 .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 似乎失败了。这个问题的解决方法是什么?

【问题讨论】:

    标签: .net docker .net-core nuget buildx


    【解决方案1】:

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

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2021-11-26
    • 2020-08-11
    • 2020-01-18
    • 2020-06-12
    • 2020-05-07
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多