8

When running 'docker build . -t project' I get this error:

Step 6/10 : RUN dotnet publish -c Release -o out
 ---> Running in 73c3f5fa9112
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 55.93 ms for /app/Backend.csproj.
/usr/share/dotnet/sdk/3.1.200/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(234,5): error NETSDK1064: Pa
ckage Microsoft.CodeAnalysis.Analyzers, version 2.9.8 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet r
estore might have only partially completed, which might have been due to maximum path length restrictions. [/app/Backend.csproj]
The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 1

My Dockerfile looks like this:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .

# Command used to start the project
ENTRYPOINT ["dotnet", "Project.dll"]

I have installed the package listed in the error message from NuGet. When I run the command 'dotnet publish -c Release -o out' in a terminal it works, but running it inside the Dockerfile keeps failing. Any ideas?

My package references:

<PackageReference Include="Microsoft.AspNetCore.Authentication.Facebook" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.2" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8" />
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />

Output from RUN dotnet restore:

Step 4/10 : RUN dotnet restore
 ---> Running in 1f26d6ac4244
  Restore completed in 47.46 sec for /app/Backend.csproj.
Removing intermediate container 1f26d6ac4244
 ---> f1a2994a2704

3
  • Hi Ahaglund, welcome to StackOverflow! I had a quick test with a template asp.net core web app, the nuget package and your Dockerfile and it seems to work for me. There may be a dependency resolution issue. Do you have a complete repro (incl. project) or could you list all the PackageReference items in the csproj? Commented Mar 18, 2020 at 22:30
  • Can you add the output of RUN dotnet restore too? Commented Mar 18, 2020 at 23:58
  • Hi Martin and Omair! I added the list of the PackageReference items and the output of RUN dotnet restore in the original message, as it was too long for a comment. Thank you! Commented Mar 19, 2020 at 6:42

1 Answer 1

9

I've just experienced the same error, which was only problematic on my local machine (everything worked well on the CI).

So it got me questioning to what was different between my machine and the CI. The answer: I had previously ran a local version of my dotnet project.

The problem for me was that the volume mount (as well as the COPY ./ . command) were including the nuget packages from my host machine into the built container. Then, obviously, the dotnet restore did not install the packages again since it thought it had them already, even though they were not for the appropriate platform (I was here running WSL 2 docker on a Windows 10 host).

To fix this, what I had to do was the following:

  1. In my docker-compose file, add a volume to take what's already inside the built container when mounting my local files for development. Here's what it looks like:
volumes:
    - ./server:/app
    - /app/obj/
    - /app/bin/
    - /app/out/
  1. Then, inside my dotnet project folder, I created a .dockerignore file to exclude the package and build related files from being COPIED at the docker build stage (also, this is what is being done according to the dotnet-docker-samples). Here is the content of the file:
bin/
obj/
out/

Hope this can help others! 😊

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.