Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include dependencies in .NET Core app docker 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).

like image 241
Sebastian Krysmanski Avatar asked Aug 05 '16 19:08

Sebastian Krysmanski


People also ask

How to deploy ASP NET Core project to Docker?

Open VS, go to File > New and select Project > ASP .NET Core Web Application. Select the solution name (e.g. Training service) and the location in your local directory. Click ok. O nce the project is created, you can either take the full attached code or create your own application to deploy it into docker. 2.

Why can't I find the dependency of a docker image?

However, when I create a Docker image from the project and try to execute the app from there, it can't find the dependency: This is to be expected because Newtonsoft.Json.dll is not being copied by Visual Studio to the output folder.

How do I run a docker container from a DotNet project?

You need a .NET Core app that the Docker container will run. Open your terminal, create a working folder if you haven't already, and enter it. In the working folder, run the following command to create a new project in a subdirectory named app: dotnet new console -o App -n NetCore.Docker Your folder tree will look like the following:

How do I build and run an aspnetapp Docker image?

Use the following commands to build and run your Docker image: $ docker build -t aspnetapp. $ docker run -d -p 8080:80 --name myapp aspnetapp View the web page running from a container 🔗 Go to localhost:8080 to access your app in a web browser.


1 Answers

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.

like image 79
Sebastian Krysmanski Avatar answered Sep 24 '22 15:09

Sebastian Krysmanski