如何将 NuGet 包添加到 Dockerfile 构建步骤?

How to add NuGet packages to the Dockerfile build steps?

我有一个带有一些 NuGet 包引用的小型 .NET Framework 4.6.2 应用程序。执行时:docker build -t myapp . 我收到错误:每个引用的 NuGet 包的 Could not resolve this reference.

我试过:

如何将 NuGet 包添加到构建过程中?

感谢您的宝贵时间!

Docker 文件:

FROM microsoft/dotnet-framework-build:4.7.1 as build-env

WORKDIR /app
COPY . /app

RUN ["dotnet", "build"]

FROM microsoft/dotnet-framework:4.7.1
WORKDIR /app
COPY --from=build-env /app .

ENTRYPOINT ["MessageProcessor.exe"]

构建步骤中单个引用的完整错误:

C:\Program Files (x86)\Microsoft Visual Studio17\BuildTools\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets(2041,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "GreenPipes, Version=1.2.1.98, Culture=neutral, PublicKeyToken=b800c4cfcdeea87b, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [C:\app\MessageProcessor.csproj]

想通了。

MSBuild (RUN ["dotnet", "restore"]) 没有恢复 NuGet 包,原因已解释 here。所以我开始尝试使用 nuget.exe 命令并且最终成功了。

FROM microsoft/dotnet-framework-build:4.7.1 as build-env

WORKDIR /app
COPY . /app
RUN nuget.exe restore MessageProcessor.csproj -SolutionDirectory ../ -Verbosity normal
RUN MSBuild.exe MessageProcessor.csproj /t:build /p:Configuration=Release /p:OutputPath=./out

FROM microsoft/dotnet-framework:4.7.1
WORKDIR /app
COPY --from=build-env app/out .

ENTRYPOINT ["MessageProcessor.exe"]

这给我留下了一个漂亮干净闪亮的 .NET Framework 4.6.2 Docker 容器。