12

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).

5
  • I'm not a .net person so forgive my ignorance... Why does adding a RUN dotnet restore prevent you from using the dotnet base image?
    – Matt
    Aug 6, 2016 at 0:34
  • @Matt is correct, it shouldn't prevent you from using the microsoft/dotnet base image. Aug 6, 2016 at 2:03
  • @Matt Because the core image doesn't contain the restore command. Aug 6, 2016 at 5:50
  • @SebastianKrysmanski oic.. so like a jre instead of jdk. Missing some dev bits.
    – Matt
    Aug 6, 2016 at 6:08
  • @Matt Yes, it's like this. The core image is just the runtime. Aug 6, 2016 at 13:08

2 Answers 2

20

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.

3

I wrote a tutorial on this recently. The Dockerfile contents I used were (slightly modified to remove the ASP.NET Core bits):

FROM microsoft/dotnet:latest
COPY . /app
WORKDIR /app

RUN ["dotnet", "restore"]
RUN ["dotnet", "build"]

ENTRYPOINT ["dotnet", "run"]

When you run docker build, it uses the Dockerfile as a "recipe" to build the image. It'll run dotnet restore and dotnet build first, then package everything up into the image. The resulting image has everything the app needs to run on any Docker host.

3
  • 1
    Unfortunately, this doesn't work with a dotnet:xxx-core image. I'm not sure, though, whether the difference between dotnet and dotnet:xxx-core as base image is relevant. Aug 6, 2016 at 13:07
  • @SebastianKrysmanski Just curious, why do you want to use dotnet:xxx-core instead of dotnet:latest? Aug 6, 2016 at 14:44
  • 1
    No special reason. It seemed to most logical/minimal solution to me. Also, I'm trying not to put the source code in the container. Aug 7, 2016 at 6:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.