繁体   English   中英

运行 dotnet publish -c Release -o out 时,在 ASP.NET Core 项目中构建 Dockerfile 失败

[英]Building Dockerfile in ASP.NET Core project fails when running dotnet publish -c Release -o out

运行 '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" />

RUN 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

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

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

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

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

  1. 在我的 docker-compose 文件中,添加一个卷以在安装我的本地文件进行开发时获取已构建容器中的内容。 这是它的样子:
volumes:
    - ./server:/app
    - /app/obj/
    - /app/bin/
    - /app/out/
  1. 然后,我DOTNET项目文件夹内,我创建了一个.dockerignore文件排除包,并从在泊坞窗构建阶段被复制建造相关的文件(也,这是正在做什么根据dotnet-docker-samples )。 这是文件的内容:
bin/
obj/
out/

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM