【问题标题】:Building Dockerfile in ASP.NET Core project fails when running dotnet publish -c Release -o out运行 dotnet publish -c Release -o out 在 ASP.NET Core 项目中构建 Dockerfile 失败
【发布时间】:2020-03-18 19:26:40
【问题描述】:

当运行 'docker build . -t project' 我得到这个错误:

Step 6/10 : RUN dotnet publish -c Release -o out
 ---> Running in 73c3f5fa9112
Microsoft (R) Build Engine version 16.5.0+d4cbfca49 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restore completed in 55.93 ms for /app/Backend.csproj.
/usr/share/dotnet/sdk/3.1.200/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(234,5): error NETSDK1064: Pa
ckage Microsoft.CodeAnalysis.Analyzers, version 2.9.8 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet r
estore might have only partially completed, which might have been due to maximum path length restrictions. [/app/Backend.csproj]
The command '/bin/sh -c dotnet publish -c Release -o out' returned a non-zero code: 1

我的 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

# 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 .

# Command used to start the project
ENTRYPOINT ["dotnet", "Project.dll"]

我已安装来自 NuGet 的错误消息中列出的包。当我在终端中运行命令“dotnet publish -c Release -o out”时它可以工作,但是在 Dockerfile 中运行它总是失败。有什么想法吗?

我的包参考:

<PackageReference Include="Microsoft.AspNetCore.Authentication.Facebook" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.2" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.2" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8" />
    <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.2" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />

运行 dotnet restore 的输出:

Step 4/10 : RUN dotnet restore
 ---> Running in 1f26d6ac4244
  Restore completed in 47.46 sec for /app/Backend.csproj.
Removing intermediate container 1f26d6ac4244
 ---> f1a2994a2704

【问题讨论】:

  • 嗨 Ahaglund,欢迎来到 StackOverflow!我使用模板 asp.net 核心 Web 应用程序、nuget 包和您的 Dockerfile 进行了快速测试,它似乎对我有用。可能存在依赖关系解析问题。您是否有完整的 repro(包括项目)或者您能否列出 csproj 中的所有 PackageReference 项?
  • 你能把RUN dotnet restore的输出也加进去吗?
  • 嗨,马丁和奥马尔!我在原始消息中添加了 PackageReference 项目的列表和 RUN dotnet restore 的输出,因为它太长了,无法发表评论。谢谢!

标签: c# docker asp.net-core .net-core nuget


【解决方案1】:

我刚刚遇到了同样的错误,这只是在我的本地机器上存在问题(在 CI 上一切正常)。

这让我开始质疑我的机器和 CI 之间有什么不同。答案:我之前运行过我的 dotnet 项目的本地版本。

对我来说,问题是 volume mount(以及 COPY ./ . 命令)将 nuget 包从我的主机包含到构建的容器中。然后,很明显,dotnet restore 没有再次安装这些软件包,因为它认为它已经有了它们,即使它们不适合适当的平台(我在这里在 Windows 10 主机上运行 WSL 2 docker)。

要解决这个问题,我必须做的是:

  1. 在我的 docker-compose 文件中,添加一个卷,以便在安装本地文件进行开发时获取已构建容器中的内容。如下所示:
volumes:
    - ./server:/app
    - /app/obj/
    - /app/bin/
    - /app/out/
  1. 然后,在我的 dotnet 项目文件夹中,我创建了一个 .dockerignore 文件,以在 docker build 阶段将包和构建相关文件排除在 COPIED 之外(另外,这就是正在做的事情according to the dotnet-docker-samples)。以下是文件内容:
bin/
obj/
out/

希望这可以帮助其他人! ?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2022-08-10
    • 2022-12-03
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    相关资源
    最近更新 更多