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

7

我有一个使用.NET Framework 4.6.2编写的小型应用程序,其中包含一些NuGet软件包引用。 在执行以下命令时:docker build -t myapp . 我会收到错误消息:无法解析此引用。 对于每个引用的NuGet软件包。

我已经尝试过:

  • 添加 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]

在我的情况下,我只需要运行RUN dotnet restore就可以了。你运行COPY . .时是否在正确的文件夹中?你的dotnet restore输出是什么? - Boas Enkler
编辑了COPY行。当我添加RUN dotnet restore时,输出如下:Restore: Nothing to do. None of the projects specified contain packages to restore. Done Building Project "C:\app\MessageProcessor.csproj" (Restore target(s)). - Wijnand
请原谅我的无知,但这不是.NET Core而不是完整的.NET框架,因此dotnet不用于构建项目吗? - Gregory Suvalian
@GregorySuvalian 这个应用程序是基于 .NET Framework 4.6.2 的项目开发的。但我希望能使用 .NET Core 作为选项... - Wijnand
1个回答

15

弄清楚了。

由于某些原因,MSBuild(RUN ["dotnet", "restore"])不会恢复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 容器。

对于那些困惑的读者,dotnet restore是用于.NET Core的CLI实用程序,而不是.NET Framework,这就是为什么需要使用像nuget.exeMSBuild.exe这样的.NET Framework实用工具的原因。 - Adilet Soronov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接