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



运行'docker build时-t项目'我得到这个错误:

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网络恢复的输出:

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没有再次安装这些包,因为它认为自己已经安装了这些包,尽管它们不适用于合适的平台(我在Windows10主机上运行WSL2docker)。

为了解决这个问题,我必须做以下事情:

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

希望这能帮助其他人!

最新更新