dotnet安装包时找不到依赖关系_如何在.NET Core应用程序Docker映像中包含依赖关系?...

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:-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:-core image.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值