由于相对路径,在 Docker 容器中运行 dotnet 在 Linux 上失败

时间:2021-02-28 14:00:49

标签: .net docker .net-5 dotnet-sdk

我有一个 Dockerfile 可以构建应用程序并将其发布到这样的容器中

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"]

构建镜像和运行容器在 Windows 上工作,但在 Ubuntu 服务器上不起作用。

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'

我认为问题在于将 dotnet 的参数指定为 ../Project.dll,因为 shell 将其扩展为 /Project.dll 并且 dotnet 不理解 Linux 路径。有没有办法解决这个问题?

我是 Docker 的初学者,也许我遗漏了一些东西(Docker 在每个平台上的工作方式不应该都一样吗?)

编辑:在父目录内的容器中从 bash 运行 dotnet Project.dll 会产生完全相同的输出

1 个答案:

答案 0 :(得分:1)

原来是权限问题,应用必须在子文件夹(不是根文件夹),见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