簡體   English   中英

Docker .NET Core - 解決方案中的多個項目

[英]Docker .NET Core - multiple projects in solution

我是 Docker 的新手,我正在嘗試容器化我的 WebAPI,它是包含多個項目的解決方案的一部分。 這是我的文件夾結構

Solution
    -- RandomPersonPicker.Api
        -- .dockerignore
        -- .dockerfile
    -- RandomPersonPicker.Date
    -- RandomPersonPicker.Domain
    -- RandomPersonPicker.Tests

這是我的 Docker 文件:

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

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ../
RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/sdk:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "RandomPersonPicker.Api.dll"]

我從 RandomPersonPicker.Api 文件夾運行這個命令

docker build -t aspnetapp

最后這是我的堆棧跟蹤:

Skipping project "/RandomPersonPicker.Data/RandomPersonPicker.Data.csproj" because it was not found.
  Skipping project "/RandomPersonPicker.Data/RandomPersonPicker.Data.csproj" because it was not found.
  Restore completed in 186.69 ms for /app/RandomPersonPicker.Api.csproj.
/usr/share/dotnet/sdk/3.1.101/Microsoft.Common.CurrentVersion.targets(1875,5): warning : The referenced project '../RandomPersonPicker.Data/RandomPersonPicker.Data.csproj' does not exist. [/app/RandomPersonPicker.Api.csproj]
CompositionRoot.cs(2,26): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'RandomPersonPicker' (are you missing an assembly reference?) [/app/RandomPersonPicker.Api.csproj]
CompositionRoot.cs(3,26): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'RandomPersonPicker' (are you missing an assembly reference?) [/app/RandomPersonPicker.Api.csproj]
Controllers/PersonController.cs(4,26): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'RandomPersonPicker' (are you missing an assembly reference?) [/app/RandomPersonPicker.Api.csproj]
Controllers/PersonController.cs(5,26): error CS0234: The type or namespace name 'Domain' does not exist in the namespace 'RandomPersonPicker' (are you missing an assembly reference?) [/app/RandomPersonPicker.Api.csproj]
Controllers/PersonController.cs(23,39): error CS0246: The type or namespace name 'Person' could not be found (are you missing a using directive or an assembly reference?) [/app/RandomPersonPicker.Api.csproj]
Controllers/PersonController.cs(31,27): error CS0246: The type or namespace name 'Person' could not be found (are you missing a using directive or an assembly reference?) [/app/RandomPersonPicker.Api.csproj]
Controllers/PersonController.cs(15,26): error CS0246: The type or namespace name 'IPersonRepository' could not be found (are you missing a using directive or an assembly reference?) [/app/RandomPersonPicker.Api.csproj]
Controllers/PersonController.cs(17,33): error CS0246: The type or namespace name 'IPersonRepository' could not be found (are you missing a using directive or an assembly reference?) [/app/RandomPersonPicker.Api.csproj]
The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 1

我理解這個問題,docker 無法識別我的其他項目的命名空間,這會導致編譯錯誤,但我不知道解決這個問題的好方法。

任何幫助,將不勝感激。

在這種情況下,我發現將 Dockerfile 放在與解決方案 (.sln) 相同的文件夾中更容易。 因此,如果您有多個具有公共類庫的項目,您可以輕松地在單獨的 docker 文件中引用它們。 您可以從 root -> root 復制所有內容,也可以一個一個地提及每個項目。

注意:這是 git 的根,因此 Dockerfile 也成為代碼存儲庫的一部分。

虛擬項目示例:

ROOT FOLDER
    |--- MultiProject.sln
    |--- MultiProject (solution folder)
        |-- Api.Web (project folder)
            -- Api.Web.csproj
        |-- Api.Mobile (project folder)
            -- Api.Mobile.csproj
        |-- ClassLibrary1
        |-- ClassLibrary2
    |--- Api.Web.Dockerfile
    |--- Api.Mobile.Dockerfile
    |--- .gitignore (*just to highlight the root of source repo*)

虛擬項目 Api.Web.Dockerfile:

注意:您可能希望重構 dockerfile 以適應您的分層風格。 示例中顯示的層具有單獨的構建和發布層。

#Use your choice of image as base. Mine is alpine! 
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-alpine AS build
WORKDIR /src
COPY . .

RUN dotnet restore "MultiProject/Api.Web.csproj"
WORKDIR "/src/."
COPY . .
RUN dotnet build "MultiProject/Api.Web.csproj" -c Release -o /app/build

FROM build as publish
RUN dotnet publish "MultiProject/Api.Web.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENV ASPNETCORE_URLS http://*:PORT_NUMBER
ENTRYPOINT ["dotnet", "Api.dll"]

現在,如果我想為 Api.Mobile 創建另一個 dockerfile,從上面的示例中創建一個非常簡單。

嘗試在不同的步驟中執行構建和發布步驟。 您還需要在恢復和構建后復制文件

下面幾行的東西

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

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore *.csproj
COPY . .
RUN dotnet build -c Release

# Copy everything else and build
COPY . ../
RUN dotnet publish -o out --no-build

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/sdk:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "RandomPersonPicker.Api.dll"]

截至 2022 年 docker-compose 3.9

經過幾次嘗試,我設法這樣做:

項目層次:

MoulaTracker/
|- MoulaTracker.Core/    # <- PROJECT 1
|- MoulaTracker.Api/     # <- PROJECT 2
|- Dockerfile.Core
|- Dockerfile.Api
|- docker-compose.yml
|- MoulaTracker.sln

MoulaTracker.Api項目依賴於MoulaTracker.Core

docker-compose.yml

version: "3.9"

services:
  moula-tracker-core:
    build:
      context: .
      dockerfile: ./Dockerfile.Core
  moula-tracker-api:
    build:
      context: .
      dockerfile: ./Dockerfile.Api

Dockerfile.Api

FROM mcr.microsoft.com/dotnet/core/sdk:3.1.409 AS build-env

# Copy csproj and restore as distinct layers
WORKDIR /MoulaTracker.Api
COPY ./MoulaTracker.Api .
COPY ./MoulaTracker.Core ../MoulaTracker.Core
RUN dotnet restore

RUN dotnet publish -c Release -o out

# Build runtime image
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1.0 AS runtime

WORKDIR /MoulaTracker.Api
COPY --from=build-env /MoulaTracker.Api/ ./

ENTRYPOINT ["dotnet", "out/MoulaTracker.Api.dll"]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM