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

1

2

I wrote a Dockerfile that uses the Docker buildx --mount=type=cache setting to cache my NuGet packages for faster builds. This seemed to work in .NET 5 as indicated by this other question.

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
...

However, in .NET 6 I get the following error:

 => [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

It seems like the dotnet restore fails. What is the solution to this problem?

Celestine answered 1/12, 2021 at 9:33 Comment(0)
F
4

You are missing the --mount=type=cache,id=nuget,target=/root/.nuget/packages in the build command

Farinose answered 16/2, 2022 at 21:42 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Strictly

© 2022 - 2024 — McMap. All rights reserved.