从我的 Dockerfile 引用父目录中的项目

Referencing a project in a parent directory from my Dockerfile

所以我有一个 .NET 6 解决方案,其结构如下:

MySolution
|
+-- RecipeManagement
|
+---- src
|
+------ RecipeManagement
|
+-------- RecipeManagement.csproj
|
+-------- Dockerfile
|
+---- tests
|
+------ RecipeManagement.IntegrationTests
|
+-------- RecipeManagement.IntegrationTests.csproj
|
+------ RecipeManagement.UnitTests
|
+-------- RecipeManagement.UnitTests.csproj
|
+-- SharedKernel
|
+---- SharedKernel.csproj
|
+-- dockercompose.yaml

我无法让我的 Dockerfile 正确引用我的 SharedKernel class 项目。

没有 SharedKernel,像这样的东西通常工作正常:

码头文件:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY ["RecipeManagement.csproj", "./"]
RUN dotnet restore "./RecipeManagement.csproj"

# Copy everything else and build
COPY . ./
RUN dotnet build "RecipeManagement.csproj" -c Release -o /app/build

FROM build-env AS publish
RUN dotnet publish "RecipeManagement.csproj" -c Release -o /app/out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .

ENV ASPNETCORE_URLS=https://+:8080
EXPOSE 8080

ENTRYPOINT ["dotnet", "/app/RecipeManagement.dll"]

撰写:

version: '3.7'

services:
  recipemanagement-db:
    image: postgres
    restart: always
    ports:
      - '52450:5432'
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=dev_recipemanagement
    volumes:
      - recipemanagement-data:/var/lib/postgresql/data

  recipemanagement-api:
    build:
      context: ./RecipeManagement/src/RecipeManagement
      dockerfile: Dockerfile
    ports:
      - "52451:8080"
    environment:
      ASPNETCORE_ENVIRONMENT: "Development"
      DB_CONNECTION_STRING: "Host=recipemanagement-db;Port=5432;Database=dev_recipemanagement;Username=postgres;Password=postgres"

    volumes:
      - ~/.aspnet/https:/https:ro
        
volumes:
  recipemanagement-data:

但是,一旦我尝试引入 SharedKernel,它就会变得不愉快。注意区别:

  1. 更新了 contextdockerfile 撰写中的路径
  2. 更新了 Copy csproj and restore as distinct layers 部分以说明新的上下文和项目

码头文件:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY ["RecipeManagement/src/RecipeManagement/RecipeManagement.csproj", "RecipeManagement/"]
COPY ["SharedKernel/SharedKernel.csproj", "SharedKernel/"]
RUN dotnet restore "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"

# Copy everything else and build
COPY . ./
RUN dotnet build "RecipeManagement.csproj" -c Release -o /app/build

FROM build-env AS publish
RUN dotnet publish "RecipeManagement.csproj" -c Release -o /app/out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .

ENV ASPNETCORE_URLS=https://+:8080
EXPOSE 8080

ENTRYPOINT ["dotnet", "/app/RecipeManagement.dll"]

撰写:

version: '3.7'

services:
  recipemanagement-db:
    image: postgres
    restart: always
    ports:
      - '52450:5432'
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=dev_recipemanagement
    volumes:
      - recipemanagement-data:/var/lib/postgresql/data

  recipemanagement-api:
    build:
      context: .
      dockerfile: RecipeManagement/src/RecipeManagement/Dockerfile
    ports:
      - "52451:8080"
    environment:
      ASPNETCORE_ENVIRONMENT: "Development"
      DB_CONNECTION_STRING: "Host=recipemanagement-db;Port=5432;Database=dev_recipemanagement;Username=postgres;Password=postgres"

    volumes:
      - ~/.aspnet/https:/https:ro
        
volumes:
  recipemanagement-data:

这里的结果是由于某种原因找不到文件:

------
 > [build 5/8] RUN dotnet restore "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj":
#13 0.374 MSBUILD : error MSB1009: Project file does not exist.
#13 0.374 Switch: RecipeManagement/src/RecipeManagement/RecipeManagement.csproj
------

Potentially of note, if i just adjust the context and dockerfile path with the original project that doesn't have a shared kernel, i start getting yelled at about my test projects not being included

编辑

为了回应@Paolo 的回答,我从我的根 MySolution 目录中尝试了以下两个 dockerfiles 运行 docker build -f RecipeManagement/src/RecipeManagement/Dockerfile . 并得到了相同的错误。我尝试了几种不同的 dockerfile 迭代。见下面两个:

一次迭代:

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY ["RecipeManagement/src/RecipeManagement/RecipeManagement.csproj", "RecipeManagement/"]
COPY ["SharedKernel/SharedKernel.csproj", "SharedKernel/"]
RUN dotnet restore "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"

# Copy everything else and build
COPY . ./
RUN dotnet build "RecipeManagement.csproj" -c Release -o /app/build

FROM build-env AS publish
RUN dotnet publish "RecipeManagement.csproj" -c Release -o /app/out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .

ENV ASPNETCORE_URLS=https://+:8080
EXPOSE 8080

ENTRYPOINT ["dotnet", "/app/RecipeManagement.dll"]

第二次迭代:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
ENV ASPNETCORE_URLS=https://+:8080
EXPOSE 8080

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["RecipeManagement/src/RecipeManagement/RecipeManagement.csproj", "RecipeManagement/"]
COPY ["SharedKernel/SharedKernel.csproj", "SharedKernel/"]
RUN dotnet restore "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"
COPY . .
WORKDIR "/src/RecipeManagement"
RUN dotnet build "RecipeManagement.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "RecipeManagement.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RecipeManagement.dll"]

** 更新:如果我改变 RUN dotnet restore "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"RUN dotnet restore "RecipeManagement/RecipeManagement.csproj"

在选项 2 中它克服了错误,但想要构建我所有的测试

好的,所以我更新了它,让复制路径反映了我的解决方案路径,这似乎可以解决问题!在 http 和 https 中也有错别字,但那是不同的:-)

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY ["RecipeManagement/src/RecipeManagement/RecipeManagement.csproj", "./RecipeManagement/src/RecipeManagement/"]
COPY ["SharedKernel/SharedKernel.csproj", "./SharedKernel/"]
RUN dotnet restore "./RecipeManagement/src/RecipeManagement/RecipeManagement.csproj"

# Copy everything else and build
COPY . ./
RUN dotnet build "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/build

FROM build-env AS publish
RUN dotnet publish "RecipeManagement/src/RecipeManagement/RecipeManagement.csproj" -c Release -o /app/out

# Build runtime image
FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY --from=publish /app/out .

ENV ASPNETCORE_URLS=http://+:8080
EXPOSE 8080

ENTRYPOINT ["dotnet", "/app/RecipeManagement.dll"]
version: '3.7'

services:
  recipemanagement-db:
    image: postgres
    restart: always
    ports:
      - '52450:5432'
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=dev_recipemanagement
    volumes:
      - recipemanagement-data:/var/lib/postgresql/data

  recipemanagement-api:
    build:
      context: .
      dockerfile: RecipeManagement/src/RecipeManagement/Dockerfile
    ports:
      - "52451:8080"
    environment:
      ASPNETCORE_ENVIRONMENT: "Development"
      DB_CONNECTION_STRING: "Host=recipemanagement-db;Port=5432;Database=dev_recipemanagement;Username=postgres;Password=postgres"

    volumes:
      - ~/.aspnet/https:/https:ro
        
volumes:
  recipemanagement-data: