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

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

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:

  1. FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
  2. WORKDIR /app
  3. EXPOSE 80
  4. EXPOSE 443
  5. FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
  6. WORKDIR /src
  7. COPY *.csproj .
  8. RUN dotnet restore "contentservice.csproj"
  9. COPY .
  10. RUN dotnet publish "contentservice.csproj" -c Release -o /publish
  11. FROM mcr.microsoft.com/dotnet/aspnet:6.0 as final
  12. WORKDIR /app
  13. COPY --from=build /publish .
  14. ENTRYPOINT ["dotnet", "contentservice.dll"]

docker-compose.yml文件:

  1. version: '3.4'
  2. services:
  3. userservice:
  4. image: ${DOCKER_REGISTRY-}userservice
  5. build:
  6. context: .
  7. dockerfile: userservice/Dockerfile
  8. ports:
  9. - "9000:80"
  10. environment:
  11. - GOOGLE_APPLICATION_CREDENTIALS=./firebase-config.json
  12. contentservice:
  13. image: ${DOCKER_REGISTRY-}contentservice
  14. build:
  15. context: .
  16. dockerfile: contentservice/Dockerfile
  17. ports:
  18. - "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

  1. - solution
  2. - contentserviceprj
  3. - Dockerfile
  4. - userserviceprj
  5. - Dockerfile
  6. - docker-compose
  7. - 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:

  1. FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
  2. WORKDIR /app
  3. EXPOSE 80
  4. EXPOSE 443
  5. FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
  6. WORKDIR /src
  7. COPY *.csproj .
  8. RUN dotnet restore "contentservice.csproj"
  9. COPY . .
  10. RUN dotnet publish "contentservice.csproj" -c Release -o /publish
  11. FROM mcr.microsoft.com/dotnet/aspnet:6.0 as final
  12. WORKDIR /app
  13. COPY --from=build /publish .
  14. ENTRYPOINT ["dotnet", "contentservice.dll"]

docker-compose.yml file:

  1. version: '3.4'
  2. services:
  3. userservice:
  4. image: ${DOCKER_REGISTRY-}userservice
  5. build:
  6. context: .
  7. dockerfile: userservice/Dockerfile
  8. ports:
  9. - "9000:80"
  10. environment:
  11. - GOOGLE_APPLICATION_CREDENTIALS=./firebase-config.json
  12. contentservice:
  13. image: ${DOCKER_REGISTSTRY-}contentservice
  14. build:
  15. context: .
  16. dockerfile: contentservice/Dockerfile
  17. ports:
  18. - "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.

    1. version: '3.4'
    2. services:
    3. userservice:
    4. build:
    5. context: ../userservice
    6. dockerfile: userservice/Dockerfile
    7. contentservice:
    8. build:
    9. context: ../contentservice
    10. 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.

    1. 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。

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

  1. version: '3.4';
  2. services:
  3. userservice:
  4. image: ${DOCKER_REGISTRY-}userservice
  5. build:
  6. context: ../
  7. dockerfile: ./userservice/Dockerfile
  8. ports:
  9. - "9000:80"
  10. environment:
  11. - GOOGLE_APPLICATION_CREDENTIALS=./firebase-config.json
  12. contentservice:
  13. image: ${DOCKER_REGISTRY-}contentservice
  14. build:
  15. context: ../
  16. dockerfile: ./contentservice/Dockerfile
  17. ports:
  18. - "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 ../ :

  1. version: '3.4'
  2. services:
  3. userservice:
  4. image: ${DOCKER_REGISTRY-}userservice
  5. build:
  6. context: ../
  7. dockerfile: ./userservice/Dockerfile
  8. ports:
  9. - "9000:80"
  10. environment:
  11. - GOOGLE_APPLICATION_CREDENTIALS=./firebase-config.json
  12. contentservice:
  13. image: ${DOCKER_REGISTSTRY-}contentservice
  14. build:
  15. context: ../
  16. dockerfile: ./contentservice/Dockerfile
  17. ports:
  18. - "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
  • .net
  • c#
  • docker
  • docker-compose

为所有缺失的枚举添加到List中。 go 97
匿名

发表评论

匿名网友

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

确定

  • 开发者交流平台

    本页二维码