【问题标题】:Problems of creating a Dockerfile for .net core application为 .net 核心应用程序创建 Dockerfile 的问题
【发布时间】:2021-02-28 20:42:36
【问题描述】:

我是 docker 新手,所以我想寻求帮助,为 .net 核心应用程序创建 Dockerfile。 该应用程序具有以下架构:

   FarmHouse.Services.AuthenticationService
    │   ├───FarmHouse.Services.AuthenticationService.Api
    │   ├───FarmHouse.Services.AuthenticationService.Logic
    │   └───Dockerfile
    │ 
    ├───FarmHouse.Services.Common
    │   └───FarmHouse.Services.Common.Models
    │   
    └───FarmHouse.Services.EmailService
        ├───FarmHouse.Services.EmailService.Api
        ├───FarmHouse.Services.EmailService.Logic
        └───Dockerfile

这三个项目(FarmHouse.Services.EmailServiceFarmHouse.Services.CommonFarmHouse.Services.AuthenticationService)在同一个解决方案中,FarmHouse.Services.AuthenticationService 使用来自 Common 项目的对象,即 FarmHouse.Services.Common 是它的依赖项。

我希望其他服务也可以链接到 Common。我的 AuthenticationService 的 Dockerfile 如下所示:

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

# Copy csproj and restore as distinct layers
COPY ./*.sln .
COPY ./FarmHouse.Services.AuthenticationService/FarmHouse.Services.AuthenticationService.Logic/FarmHouse.Services.AuthenticationService.Logic.csproj ./FarmHouse.Services.AuthenticationService.Logic/FarmHouse.Services.AuthenticationService.Logic.csproj
COPY ./FarmHouse.Services.AuthenticationService/FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj ./FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj
COPY ./FarmHouse.Services.Common/FarmHouse.Services.Common.Models/FarmHouse.Services.Common.Models.csproj ./FarmHouse.Services.Common.Models/FarmHouse.Services.Common.Models.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/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "FarmHouse.Services.AuthenticationService.Api.dll"]

当我从父文件夹运行 docker build 时,我看到以下错误:

>docker build -t coreapp -f ./FarmHouse.Services.AuthenticationService/Dockerfile .

#14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.AuthenticationService/FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj" was not found. [/app/FarmHouseServices.sln]417050dbf1abf19fd7c8a7a0eb5d56150a                                                         0.0s
    #14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.AuthenticationService/FarmHouse.Services.AuthenticationService.Logic/FarmHouse.Services.AuthenticationService.Logic.csproj" was not found. [/app/FarmHouseServices.sln]                                                                                       0.0s
    #14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.Common/FarmHouse.Services.Common.Models/FarmHouse.Services.Common.Models.csproj" was not found. [/app/FarmHouseServices.sln]uthenticationService/FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj ./FarmHous  0.0s
    #14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.EmailService/FarmHouse.Services.EmailService.Api/FarmHouse.Services.EmailService.Api.csproj" was not found. [/app/FarmHouseServices.sln]                                                                                                                      1.0s
    #14 1.643 /usr/share/dotnet/sdk/3.1.406/NuGet.targets(282,5): error MSB3202: The project file "/app/FarmHouse.Services.EmailService/FarmHouse.Services.EmailService.Logic/FarmHouse.Services.EmailService.Logic.csproj" was not found. [/app/FarmHouseServices.sln]

请建议在这种情况下如何修改 Dockerfile 或架构。 谢谢。

【问题讨论】:

    标签: asp.net .net docker .net-core


    【解决方案1】:

    您收到这些错误是因为您在.sln 文件级别运行dotnet 命令,因此dotnet 尝试恢复解决方案中尚未复制的所有.csproj 文件。

    尝试将您的 dotnet restorepublish 命令替换为:

    RUN dotnet restore ./FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj
    
    RUN dotnet publish ./FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj -c Release -o out
    

    您也可以复制所有 .csproj 文件并使用简单的 dotnet restore 恢复它们,但无论如何我都会使用 dotnet publish 命令的项目路径。

    【讨论】:

    • 当我尝试执行此操作时,我收到以下警告:#17 4.032 /usr/share/dotnet/sdk/3.1.406/Microsoft.Common.CurrentVersion.targets(2084,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "FarmHouse.Services.Common.Models". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [/app/FarmHouse.Services.AuthenticationService.Api/FarmHouse.Services.AuthenticationService.Api.csproj].然后出现错误,指出未找到来自 Common 项目的类型。
    • 您需要复制FarmHouse.Services.AuthenticationService.Api.csproj 中引用的项目中出现的任何文件夹结构,否则这些相对路径将不起作用。复制公共项目时,您似乎缺少FarmHouse.Services.Common 部分。
    • 回到问题,因为我决定在 gitlab 中创建管道。在本地,docker build 运行良好,但 gitlab 不行:before_script: - apk add --no-cache --repository http://dl-cdn.alpinelinux.org/alpine/edge/community nodejs-npm - npm i -g heroku - docker login -u _ -p token registry.heroku.com script: - cd FarmHouseServices - docker build -f ./FarmHouse.Services.AuthenticationService/Dockerfile .
    • 随意创建一个单独的问题,仅使用 cmets 很难理解新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2020-03-30
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    相关资源
    最近更新 更多