Search code examples
.netdockerdockerfilerider

implicit package loading in dotnet with docker


I have a .NET Project: TestApp. I can get it to run successfully using a Rider run configuration and Target framework net5.0 (on a Mac). I am trying to containerize it with Docker.

In this case, the .csproj file for the TestApp does not explicitly name all of the packages needed; other required packages are loaded implicitly when the corresponding Projects are referenced. That is, my Project references other Projects in the same Solution.

I have generated what I think is a standard Dockerfile, according to the documentation:

FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["TestApp/TestApp.csproj", "TestApp/"]
RUN dotnet restore "TestApp/TestApp.csproj"
COPY . .
WORKDIR "/src/TestApp"
RUN dotnet build "TestApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "TestApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TestApp.dll"]

When running the app from the Dockerfile, it fails on the first implicitly referenced package. In this case it is an Azure package:

Could not load file or assembly 'Azure.Core, Version=1.20.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8'. The system cannot find the file specified. System.IO.FileNotFoundException: Could not load file or assembly 'Azure.Core, Version=1.20.0.0, Culture=neutral, PublicKeyToken=92742159e12e44c8'. The system cannot find the file specified.

BUT, when I look at the container files, I can see the missing package in the /app directory:

/app/Azure.Core.dll

So, I am not sure what I'm missing here. I can add the appropriate PackageReference to the TestApp.csproj file, but I'll be chasing a lot of redundant dependencies that are already listed elsewhere in the Solution.

I thought that opening a terminal in the container and seeing what I could learn from calling dotnet might help, but I'm bumping into the fact that final doesn't have the rest of the SDK in it.

Any advice on how to troubleshoot this would be very helpful!


Solution

  • If your project references other projects in the same solution, then you need to copy all the .csproj files before doing dotnet restore. I.e.

    COPY ["TestApp/TestApp.csproj", "TestApp/"]
    

    should be

    COPY ["TestApp/TestApp.csproj", "TestApp/"]
    COPY ["Project2/Project2.csproj", "Project2/"]
    COPY ["Project3/Project3.csproj", "Project3/"]
    

    You only need to restore, build and publish the main project. It'll see the project references and include them.