简体   繁体   English

Docker .NET Core - 解决方案中的多个项目

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

I'm new to Docker and I'm trying to containerise my WebAPI which is part of a solution with multiple projects.我是 Docker 的新手,我正在尝试容器化我的 WebAPI,它是包含多个项目的解决方案的一部分。 This is my folder structure这是我的文件夹结构

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

This is my Docker file:这是我的 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"]

I run this command from RandomPersonPicker.Api folder我从 RandomPersonPicker.Api 文件夹运行这个命令

docker build -t aspnetapp

Finally this is my stack trace:最后这是我的堆栈跟踪:

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

I understand the problem, docker doesn't recognise the namespaces of my other projects which gives a compilation error, but I don't know a good way to solve this.我理解这个问题,docker 无法识别我的其他项目的命名空间,这会导致编译错误,但我不知道解决这个问题的好方法。

Any help would be appreciated.任何帮助,将不胜感激。

In cases like these, I find it easier to put the Dockerfile in the same folder as of the solution (.sln).在这种情况下,我发现将 Dockerfile 放在与解决方案 (.sln) 相同的文件夹中更容易。 So if you have multiple projects that have common class libraries, you could easily reference them in your individual docker file.因此,如果您有多个具有公共类库的项目,您可以轻松地在单独的 docker 文件中引用它们。 You could either copy everything from root -> root, or mention each project one by one.您可以从 root -> root 复制所有内容,也可以一个一个地提及每个项目。

Note: This is the root for git and so the Dockerfile becomes a part of the code repo as well.注意:这是 git 的根,因此 Dockerfile 也成为代码存储库的一部分。

Dummy Project Example:虚拟项目示例:

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*)

Dummy Project Api.Web.Dockerfile:虚拟项目 Api.Web.Dockerfile:

Note: you might want to restructure the dockerfile to suite your layering style.注意:您可能希望重构 dockerfile 以适应您的分层风格。 The one shown in the example has separate build and publish layers.示例中显示的层具有单独的构建和发布层。

#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"]

Now, if I want to create another dockerfile for Api.Mobile, it's pretty straightforward from the above example to create one.现在,如果我想为 Api.Mobile 创建另一个 dockerfile,从上面的示例中创建一个非常简单。

try to do the build and publish steps in different steps.尝试在不同的步骤中执行构建和发布步骤。 also you need to copy the files after restore and build您还需要在恢复和构建后复制文件

somthing on the lines of below下面几行的东西

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"]

As of 2022 docker-compose 3.9截至 2022 年 docker-compose 3.9

After few attempts, I managed to do it this way:经过几次尝试,我设法这样做:

Project hierarchy:项目层次:

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

The project MoulaTracker.Api is dependent of MoulaTracker.Core MoulaTracker.Api项目依赖于MoulaTracker.Core

docker-compose.yml : 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 : 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.

相关问题 asp.net core 2.0 - 多项目解决方案 docker 文件 - asp.net core 2.0 - multiple projects solution docker file 解决方案 .NET 中 Docker 多个项目的问题 - Problems with Docker multiple projects in solution .NET Docker 映像创建 - Net core 3.1 web API - 具有多个项目的单一解决方案 - Docker image creation - Net core 3.1 web API - Single solution with multiple projects .Net 核心 docker 构建与多个项目 - .Net core docker build with multiple projects 从具有多个项目的解决方案构建 docker 容器 - Build docker container from solution with multiple projects ASP.NET 5:使用多项目解决方案构建docker - ASP.NET 5: docker build with multi-projects solution Docker构建引用不同文件夹中项目的.net核心应用程序 - Docker building a .net core application that references projects in different folder 从多项目点网核心解决方案构建 Docker 镜像 - Building a Docker image from a multi project dot net core solution .Net Core 5 API 项目在 docker 容器中不能与 docker 运行或 ZBAEDB53E845AE71F13945FCC000 一起工作 - .Net Core 5 API projects in docker containers not working with docker run or docker-compose - no errors 如何在 .net 项目的 docker 构建完成后启动多个微服务? - How to start multiple microservices after docker build finished for .net projects?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM