Error building Dot Net Core 2.2 Docker Image from a Docker File
MSBUILD: error MSB1009: Project file does not exist.
ERROR: Service 'ServiceName' failed to build: The command '/bin/sh -c dotnet restore "ProjectName.csproj"' returned a non-zero code: 1
Solution: Make sure that your Dockerfile is in the root of your project as well as your docker-compose.yml file for the settings below in the code section (see Dockerfile Settings) to work.
- You build the Docker Image by issuing the command sudo docker-compose build or in some cases if you want to build and run the container all at once then do: sudo docker-compose up in the folder where docker-compose.yml is located. Remember that docker-compose.yml should include the correct path to the Dockerfile.
[Note] It is more likely that for the error to have occurred, you attempted to build an image that failed to build in the first place (This is possible when you try to run the container without building the image first by sudo docker-compose up instead of sudo docker-compose build). In this case, you need to remove all unused images and containers to start afresh: sudo system prune -a the docker system to delete unused docker images.
If you don't prune the images and unused docker containers the second command for building the image won't work unless at least at this time of writing. You clean the Docker System by issuing the command: sudo docker system prune -a this will prompt for confirmation and then confirm up by typing "Y" in the console.
- You can check to see if orphaned images are indeed cleared by: sudo docker images then proceed by building the image: sudo docker-compose build
FROM mcr.microsoft.com/dotnet/core/runtime:2.2-stretch-slim AS base WORKDIR /app EXPOSE PortNumberOpenedOnDatabaseServer FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build WORKDIR /src COPY ["ProjectName.csproj", "ProjectName/"] RUN dotnet restore "./ProjectName.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "ProjectName.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "ProjectName.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "ProjectName.dll"]
-If the above does not work for you try editing the part that says:COPY ["ProjectName.csproj", "ProjectName/"] with the code below:
COPY ["ProjectName.csproj", ""]