gpt4 book ai didi

docker - 挂载卷 .NET Core Docker

转载 作者:行者123 更新时间:2023-12-02 11:51:53 25 4
gpt4 key购买 nike

我有以下 Dockerfile 来创建 .NET Core 2.1 应用程序:

FROM microsoft/dotnet:2.1.402-sdk AS builder
WORKDIR /app
# copy csproj and restore as distinct layers
COPY . .
RUN dotnet restore ./myproject.sln
# copy everything else and build
COPY . .
RUN dotnet publish ./myproject/myproject.csproj -c Release -o /app/out

# build runtime image
FROM microsoft/dotnet:2.1.4-aspnetcore-runtime
WORKDIR /app
COPY --from=builder /app/out ./
ENV ASPNETCORE_ENVIRONMENT Production
ENTRYPOINT ["dotnet", "myproject.dll"]

我创建了 Docker 镜像,并且可以毫无问题地实例化容器。当我尝试在此容器与另一个容器之间共享数据时,我创建以下 docker-compose 文件:

version: "2"

services:
another_app:
restart: always
image: another_app:latest
container_name: another_app
ports:
- "4000"
volumes:
- shared-folder:/dist

myproject_app:
restart: always
image: myproject:latest
container_name: myproject
volumes:
- shared-folder:/app

volumes:
shared-folder:

这样配置是行不通的。我收到该应用程序的一条奇怪的 .NET 消息:

myproject_app | Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
myproject_app | http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
another_app| 0|another-a | Node Express server listening on http://localhost:4000

现在,我发现当我不在应用程序的根目录中定义卷时,问题就消失了。例如,如果我这样做:

myproject_app:
restart: always
image: myproject:latest
container_name: myproject
volumes:
- shared-folder:/app/another-folder

然后就可以了。为什么不能在 .NET Core 应用程序的根级别挂载卷以及为什么会收到该错误?

最佳答案

我遇到过类似的问题,我想我知道原因:

Configuring that way it does not work. I get a weird .NET message for that app:

myproject_app | Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
myproject_app | http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
another_app| 0|another-a | Node Express server listening on http://localhost:4000

出现此消息的原因是 dotnet 工具找不到 .dll 文件,因此它将其视为 dotnet 子命令,然后抛出错误,因为它需要 dotnet sdk。 (因为最后一次构建仅包含运行时)

主要问题

问题在于使用了 docker 卷 shared-folder:/appWORKDIR /app。这里真正发生的是,当连接 docker 卷时,它会使用中的 shared-folder 目录覆盖容器 /app 目录的内容本地计算机,因此无法找到 app .dll 文件并发生上述错误。

这就是为什么当您将其更改为 shared-folder:/app/another-folder 时,它能够完美工作,因为它映射到容器中的空目录。

关于docker - 挂载卷 .NET Core Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55040475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com