挂载卷.NET Core Docker

Car*_*las 6 docker .net-core docker-compose asp.net-core-2.1

我下面Dockerfile创建一个.NET Core 2.1 APP:

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"]
Run Code Online (Sandbox Code Playgroud)

我创建了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:
Run Code Online (Sandbox Code Playgroud)

以这种方式配置将无法正常工作。我收到该应用程序的怪异.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 
Run Code Online (Sandbox Code Playgroud)

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

myproject_app:
    restart: always
    image: myproject:latest
    container_name: myproject
    volumes:
      - shared-folder:/app/another-folder
Run Code Online (Sandbox Code Playgroud)

然后就可以了。为什么不能在.NET Core应用程序的根级别安装卷,为什么会出现该错误?

小智 9

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

以这种方式配置它不起作用。我收到该应用程序的奇怪 .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 
Run Code Online (Sandbox Code Playgroud)

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

主要问题

问题就出在泊坞窗卷的使用shared-folder:/appWORKDIR福祉/app。这里真正发生的是,当连接 docker 卷时,它会本地机器中/appshared-folder目录覆盖容器目录的内容,因此无法找到应用程序.dll文件,并且发生了上述错误。

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