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

时间:2018-02-06 12:20:00

标签: .net docker docker-for-windows windows-container

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

我试过了:

  • 添加RUN ["dotnet", "restore"]以从.csproj恢复包
  • 将图片代码更改为:4.6.2

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

谢谢你的时间!

Dockerfile:

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 Studio\2017\BuildTools\MSBuild\15.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]

1 个答案:

答案 0 :(得分:4)

想出来。

MSBuild(RUN ["dotnet", "restore"])由于here解释的某些原因而无法恢复NuGet包。所以我开始玩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容器。