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

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


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