Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Dockerfile in ASP.NET Core project fails when running dotnet publish -c Release -o out

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

like image 617
Ahaglund Avatar asked Mar 18 '20 19:03

Ahaglund


1 Answers

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! 😊

like image 54
xWiiLLz Avatar answered Nov 04 '22 06:11

xWiiLLz