Search code examples
.netdocker.net-5dotnet-sdk

Running dotnet in a Docker container fails on Linux because of relative path


I have a Dockerfile that builds and publishes an application to a container like this

FROM mcr.microsoft.com/dotnet/sdk:5.0 as builder
COPY . ./
RUN dotnet publish Project -c Release -o ./publish

FROM mcr.microsoft.com/dotnet/runtime:5.0 as runner
COPY --from=builder  /publish .
WORKDIR data
ENTRYPOINT ["dotnet", "../Project.dll"]

Building the image and running the container works on Windows, but does not work on Ubuntu server.

project_1  | Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly '/Project.dll'. The system cannot find the file specified.
project_1  |
project_1  | File name: '/Project.dll'

I think the problem is in specifying the argument for dotnet as ../Project.dll, because the shell expands this to /Project.dll and dotnet does not understand Linux paths. Is there a way to fix this?

I'm a beginner to Docker, maybe I'm missing something (shouldn't Docker work the same on every platform?)

EDIT: running dotnet Project.dll from bash in the container inside the parent directory produces the same exact output


Solution

  • Turned out to be a permission problem, and the application must be in a subfolder (not in the root folder), see https://docs.docker.com/engine/examples/dotnetcore/

    FROM mcr.microsoft.com/dotnet/sdk:5.0 as builder
    WORKDIR /app
    COPY . ./
    RUN dotnet publish Project -c Release -o ./publish
    
    FROM mcr.microsoft.com/dotnet/runtime:5.0 as runner
    WORKDIR /app
    COPY --from=builder  /app/publish .
    WORKDIR /app/data
    CMD ../Project