0

I have a .NET 6 Console project that includes 2 binaries as linked files:

 <ItemGroup>
    <None Include="..\HFMath\bin\HFMath.dll" Link="HFMath.dll">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Include="..\HFMath\bin\libHFMath.so" Link="libHFMath.so">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

I need to deploy it as a docker container. The Dockerfile scaffolded by Visual Studio is

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

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

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

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

when I run docker build, I get the following error:

#15 1.054   Determining projects to restore...
#15 1.322   All projects are up-to-date for restore.
#15 2.552 /usr/share/dotnet/sdk/6.0.100/Microsoft.Common.CurrentVersion.targets(5100,5): error MSB3030: Could not copy the file "/src/HFMath/bin/libHFMath.so" because it was not found. [/src/ConsoleApp1/ConsoleApp1.csproj]
#15 2.552 /usr/share/dotnet/sdk/6.0.100/Microsoft.Common.CurrentVersion.targets(5100,5): error MSB3030: Could not copy the file "/src/HFMath/bin/HFMath.dll" because it was not found. [/src/ConsoleApp1/ConsoleApp1.csproj]
#15 2.556
#15 2.556 Build FAILED.

I know that docker doesn't support symlinks, but I still need to build the image. How do I fix this (without changing the .NET project)?

Franco Tiveron
  • 2,364
  • 18
  • 34
  • Are the files being copied into the container and at the path specified in the error? The error messages seems to indicate they aren't. – Simply Ged Nov 22 '21 at 03:47
  • @SimplyGed the dockerfile is scaffolded by Visual Studio. not manually created. It should take care of what is needed – Franco Tiveron Nov 22 '21 at 21:33
  • Is the HFMath.dll stored under the `src` folder for your project e.g. `src\HFMath\bin\HFMath.dll? Based on the `.csproj` file and the error from Docker it looks like it is outside of `src`? Docker is copying files from the `src` folder for the build `COPY . .` so if the file is not inside the `src` folder it will not be copied. Even though VS created the Dockerfile, does not guarantee that it copies all the files, especially if they are outside of the `build context`. https://stackoverflow.com/questions/27068596/how-to-include-files-outside-of-dockers-build-context HTH – Simply Ged Nov 23 '21 at 02:58
  • @SimplyGed Please note that the question is not "why it doesn't work", but "how do I fix it". Would you be able to show a Dockerfile I can use to fix this issue, without touching the project? – Franco Tiveron Nov 23 '21 at 04:21

1 Answers1

1

Problem

If you have auto generated the Dockerfile using Visual Studio, it will also have created a .dockerignore file (usually in the .sln folder). That file will contain an entry to exclude **/bin from the build context.

This means when the COPY . . executes, it is ignoring the files in any bin folder

Solution

  1. Find and edit the .dockerignore file.
  2. Add the line !HFMath/bin/ - this will tell Docker to NOT ignore the HFMath/bin folder
  3. Run your docker build command again
Simply Ged
  • 8,250
  • 11
  • 32
  • 40