如何在.NET Core应用程序Docker映像中包含依赖关系? [英] How to include dependencies in .NET Core app docker image?
问题描述
我正在尝试构建一个.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映像中包含依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!