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?