Docker-compose 无法找到服务的 csproj 文件。

huangapple go评论65阅读模式
英文:

Docker-compose can't find service csproj file

问题

我对Docker非常陌生,通过其他问题以及Reddit上的搜索做了很多工作。我遇到的问题是Docker找不到我尝试容器化的服务的.csproj文件。当我在给定服务的解决方案中时,运行终端中的docker build成功创建了一个镜像。但是,当我尝试运行"docker-compose -f docker-compose.yml up --build"时,它给了我以下错误:

"RUN dotnet restore "contentservice.csproj, error MSb1009: 项目文件不存在"

供参考,这是我的项目结构:

  • 解决方案
    • contentserviceprj
      • Dockerfile
    • userserviceprj
      • Dockerfile
    • docker-compose
      • docker-compose.yml

这是其中一个Dockerfile(它们基本相同,只是名称不同)以及我的docker-compose.yml文件:

Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY *.csproj .
RUN dotnet restore "contentservice.csproj"
COPY .
RUN dotnet publish "contentservice.csproj" -c Release -o /publish

FROM mcr.microsoft.com/dotnet/aspnet:6.0 as final
WORKDIR /app
COPY --from=build /publish .
ENTRYPOINT ["dotnet", "contentservice.dll"]

docker-compose.yml文件:

version: '3.4'

services:
    userservice:
      image: ${DOCKER_REGISTRY-}userservice
      build:
        context: .
        dockerfile: userservice/Dockerfile
      ports:
        - "9000:80"
      environment:
        - GOOGLE_APPLICATION_CREDENTIALS=./firebase-config.json
    contentservice:
      image: ${DOCKER_REGISTRY-}contentservice
      build:
        context: .
        dockerfile: contentservice/Dockerfile
      ports:
        - "9001:80"

提前感谢!

英文:

I am very new to docker and did a good bit of searching through other questions asked here as well as reddit. The issue I am having is that docker can't find my .csproj files of the services I am trying to containerize. When I am inside the solution of a given service, running docker build in the terminal creates an image successfully. However, when I try to do "docker-compose -f docker-compose.yml up --build" it gives me the following error:

"RUN dotnet restore "contentservice.csproj, error MSb1009: project file does not exist"

For reference, here's my project structure

- solution
    - contentserviceprj
        - Dockerfile
    - userserviceprj
        - Dockerfile
    - docker-compose
        - docker-compose.yml

And here's one of the Dockerfiles (they are basically the same apart from names) and my docker-compose.yml file:

Dockerfile:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY *.csproj .
RUN dotnet restore "contentservice.csproj"
COPY . .
RUN dotnet publish "contentservice.csproj" -c Release -o /publish

FROM mcr.microsoft.com/dotnet/aspnet:6.0 as final
WORKDIR /app
COPY --from=build /publish .
ENTRYPOINT ["dotnet", "contentservice.dll"]

docker-compose.yml file:

version: '3.4'

services:
    userservice:
      image: ${DOCKER_REGISTRY-}userservice
      build:
        context: .
        dockerfile: userservice/Dockerfile
      ports:
        - "9000:80"
      environment:
        - GOOGLE_APPLICATION_CREDENTIALS=./firebase-config.json
    contentservice:
      image: ${DOCKER_REGISTSTRY-}contentservice
      build:
        context: .
        dockerfile: contentservice/Dockerfile
      ports:
       - "9001:80"

Thank you in advance!

答案1

得分: 2

代码部分不翻译,以下是翻译的部分:

  1. 改变上下文为项目文件夹的名称。

  2. 保持上下文为“.”,将docker-compose.yml文件移动到解决方案目录,并更改Dockerfile以相对于解决方案目录复制csproj文件。

我建议您选择选项2,因为如果您的项目有项目引用,您将需要复制多个csproj文件。将范围缩小到单个项目目录将不允许您执行此操作。

英文:

Your context is . for both Dockerfile builds. This means the root of the context is the docker-compose folder, not each individual project folder. When Docker tries to execute COPY *.csproj ., which is searching only in the top-level directory in the context for the file, it cannot find the file to copy.

There are a few ways of changing this:

  1. Change the context to the name of the project folder.

    version: '3.4'
    
    services:
        userservice:
          build:
            context: ../userservice
            dockerfile: userservice/Dockerfile
        contentservice:
          build:
            context: ../contentservice
            dockerfile: contentservice/Dockerfile
    

    Use this option if you don't want to change the Dockerfile.

  2. Keep the context as ., move the docker-compose.yml file to the Solution directory, and change the Dockerfile to copy the csproj file relative to the Solution directory.

    COPY contentservice/*.csproj .
    

I'd suggest you stick with option 2, because if you ever have a project that has a project reference, you'll need to copy in multiple csproj files. Narrowing the scope to a single project directory won't allow you to do that.

答案2

得分: 1

你需要在 docker-compose.yaml 文件中更改上下文。一个 . 引用了 docker-compose.yaml 文件的当前目录,但由于它位于不同的子文件夹中,无法找到 Dockerfile

目前,它会在 ./docker-compose/Dockerfile 而不是 ./contentservice/Dockerfile./userservice/Dockerfile 中查找 Dockerfile。

正确的上下文可能是 ../

version: '3.4';

services:
    userservice:
      image: ${DOCKER_REGISTRY-}userservice
      build:
        context: ../
        dockerfile: ./userservice/Dockerfile
      ports:
        - "9000:80"
      environment:
        - GOOGLE_APPLICATION_CREDENTIALS=./firebase-config.json
    contentservice:
      image: ${DOCKER_REGISTRY-}contentservice
      build:
        context: ../
        dockerfile: ./contentservice/Dockerfile
      ports:
       - "9001:80"

更多信息,请参阅有关将上下文设置为父文件夹的问题

英文:

You have to change the context in the docker-compose.yaml-file. A . reference the current directory of the docker-compose.yaml, but since it is in a different sub-folder as your services, it can't find the Dockerfile.

Currently, it would look for the Dockerfiles in ./docker-compose/Dockerfile instead of ./contentservice/Dockerfile and ./userservice/Dockerfile.

The correct context would probably be ../ :

version: '3.4'

services:
    userservice:
      image: ${DOCKER_REGISTRY-}userservice
      build:
        context: ../
        dockerfile: ./userservice/Dockerfile
      ports:
        - "9000:80"
      environment:
        - GOOGLE_APPLICATION_CREDENTIALS=./firebase-config.json
    contentservice:
      image: ${DOCKER_REGISTSTRY-}contentservice
      build:
        context: ../
        dockerfile: ./contentservice/Dockerfile
      ports:
       - "9001:80"

For more information, see this question about setting the context to a parent folder

huangapple
  • 本文由 发表于 2023年3月15日 21:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定