Permalink
Switch branches/tags
Nothing to show
Find file Copy path
8c992e2 Jun 14, 2018
1 contributor

Users who have contributed to this file

69 lines (44 sloc) 3.75 KB

Build .NET Core Self-Contained Applications with Docker

You can build .NET Core self-contained apps with Docker. Self-contained apps are a great option if you do not want to take a dependence on the .NET Runtime, either as a global install or the .NET Core Runtime Image. These instructions are based on the .NET Core Docker Sample.

Multiple variations of this sample have been provided, as follows. Some of these example Dockerfiles are demonstrated later. Specify an alternate Dockerfile via the -f argument.

Context on the IL Linker

The .NET team has built an IL linker to reduce the size of .NET Core applications. It is built on top of the excellent and battle-tested mono linker. The Xamarin tools also use this linker.

In trivial cases, the linker can reduce the size of applications by 50%. The size wins may be more favorable or more moderate for larger applications. The linker removes code in your application and dependent libraries that are not reached by any code paths. It is effectively an application-specific dead code analysis.

Add a Reference to the IL Linker

You first need to add a reference to the linker package to take advantage of IL linking, using the following instructions.

dotnet add package ILLink.Tasks -v 0.1.5-preview-1461378 -s https://dotnet.myget.org/F/dotnet-core/api/v3/index.json

The various "self-contained" Dockerfiles add the linker package to the sample project, as demonstrated in the following examples.

Building the Sample for Windows Nano Server with Docker

You can build and run the sample in a Nano Server container using the following commands. The instructions assume that you are in the root of the repository.

cd samples
cd dotnetapp
docker build --pull -t dotnetapp:nanoserver-selfcontained -f Dockerfile.nanoserver-x64-selfcontained .

You can test the image with the following instructions.

docker run --rm dotnetapp:nanoserver-selfcontained

Building the Sample for Linux X64 with Docker

You can build and run the sample in a Debian container using the following commands. The instructions assume that you are in the root of the repository.

cd samples
cd dotnetapp
docker build --pull -t dotnetapp:alpine-x64-selfcontained -f Dockerfile.alpine-x64-selfcontained .

You can test the image with the following instructions.

docker run --rm dotnetapp:alpine-x64-selfcontained

Build and run the sample for Linux ARM32 with Docker

You can build and run the sample for ARM32 and Raspberry Pi with Build .NET Core Applications for Raspberry Pi with Docker instructions. To create a self-contained application with those instructions, you need to use the Dockerfile.debian-arm32-selfcontained Dockerfile.

More Samples