如何在.NET Core应用程序Docker映像中包含依赖关系?

编程入门 行业动态 更新时间:2024-10-11 19:19:56
本文介绍了如何在.NET Core应用程序Docker映像中包含依赖关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试构建一个.NET Core应用程序停靠点映像。但是我不知道我应该如何将项目的NuGet依赖项映射到映像中。

为简单起见,我创建了一个.NET Core控制台应用程序:

using System; 使用Newtonsoft.Json; 命名空间ConsoleCoreTestApp { public class Program { public static void Main(string [] args) { Console.WriteLine($Hello World:{JsonConvert.False}); } } }

它只有一个NuGet依赖在 Newtonsoft.Json 。当我从Visual Studio运行应用程序时,一切正常。

然而,当我从项目中创建一个Docker映像并尝试从那里执行应用程序时,找不到依赖关系:

#dotnet ConsoleCoreTestApp.dll 错误:未找到依赖性清单中指定的程序集 - 软件包:'Newtonsoft.Json',版本:'9.0.1',路径:'lib / netstandard1.0 / Newtonsoft.Json.dll'

这是预期的,因为 Newtonsoft.Json.dll 没有被Visual Studio复制到输出文件夹。 p>

这里是我使用的 Dockerfile

FROM microsoft / dotnet:1.0.0-core COPY bin / Debug / app

有没有一个建议的方法来处理这个问题?

我不想运行 dotnet restore 在容器内部(因为我不想每次重新下载所有依赖项容器运行)。

我想我可以添加一个 RUN dotnet restore 条目到 Dockerfile ,但是我不能再使用 microsoft / dotnet:< version> -core 作为基础图像。

我找不到使Visual Studio将所有依赖关系复制到输出文件夹中的方法(就像常规.NET Framework项目一样)。

解决方案

经过一些阅读,我终于弄清楚了。

而不是 dotnet build 你运行:

dotnet发布

这将把所有文件(包括依赖项)放在发布文件夹中。而这个文件夹可以直接使用 microsoft / dotnet:< version> -core image。

I'm trying to build a .NET Core app docker image. But I can't figure out how I'm supposed to get the project's NuGet dependencies into the image.

For simplicity reasons I've create a .NET Core console application:

using System; using Newtonsoft.Json; namespace ConsoleCoreTestApp { public class Program { public static void Main(string[] args) { Console.WriteLine($"Hello World: {JsonConvert.False}"); } } }

It just has one NuGet dependency on Newtonsoft.Json. When I run the app from Visual Studio, everything works fine.

However, when I create a Docker image from the project and try to execute the app from there, it can't find the dependency:

# dotnet ConsoleCoreTestApp.dll Error: assembly specified in the dependencies manifest was not found -- package: 'Newtonsoft.Json', version: '9.0.1', path: 'lib/netstandard1.0/Newtonsoft.Json.dll'

This is to be expected because Newtonsoft.Json.dll is not being copied by Visual Studio to the output folder.

Here's the Dockerfile I'm using:

FROM microsoft/dotnet:1.0.0-core COPY bin/Debug /app

Is there a recommended way of dealing with this problem?

I don't want to run dotnet restore inside of the container (as I don't want to re-download all dependencies everytime the container runs).

I guess I could add a RUN dotnet restore entry to the Dockerfile but then I couldn't use microsoft/dotnet:<version>-core as base image anymore.

And I couldn't find a way to make Visual Studio copy all dependencies into the output folder (like it does with regular .NET Framework projects).

解决方案

After some more reading I finally figured it out.

Instead of dotnet build you run:

dotnet publish

This will place all files (including dependencies) in a publish folder. And this folder then can be used directly with a microsoft/dotnet:<version>-core image.

更多推荐

如何在.NET Core应用程序Docker映像中包含依赖关系?

本文发布于:2023-11-13 23:04:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:映像   应用程序   关系   如何在   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!