This .NET Core Docker sample demonstrates a best practice pattern for building Docker images for .NET Core apps for production. The sample works with both Linux and Windows containers.
The sample Dockerfile creates an .NET Core application Docker image based off of the .NET Core Runtime Docker base image.
It uses the Docker multi-stage build feature to build the sample in a container based on the larger .NET Core SDK Docker base image and then copies the final build result into a Docker image based on the smaller .NET Core Docker Runtime base image. The SDK image contains tools that are required to build applications while the runtime image does not.
This sample requires Docker 17.06 or later of the Docker client. You need the latest Windows 10 or Windows Server 2016 to use Windows containers. The instructions assume you have the Git client installed.
The easiest way to get the sample is by cloning the samples repository with git, using the following instructions. You can also just download the repository (it is small) as a zip from the .NET Core Docker samples respository.
git clone https://github.com/dotnet/dotnet-docker-samples/
You can build and run the sample in Docker using the following commands. The instructions assume that you are in the root of the repository.
cd dotnetapp-prod
docker build -t dotnetapp-prod .
docker run --rm dotnetapp-prod Hello .NET Core from Docker
Note: The instructions above work for both Linux and Windows containers. The .NET Core docker images use multi-arch tags, which abstract away different operating system choices for most use-cases.
The goal of this section is to create and run a Docker .NET Core runtime-based image on a Raspberry Pi running Linux. The .NET Core SDK does not run on the Linux + ARM32 configuration. As a result, the instructions used for X64 don't work. There are multiple ways to get around this limitation, primarily:
- Build app on X64 and copy via scp (or pscp) to ARM32 device and then build and run a Docker runtime image on the ARM32 device, or
- Build final ARM32 image on Windows, push image to a Docker registry and then pull and run from the ARM32 device.
The second option is only supported on Windows. Linux and macOS user must use the first option. For simplicity, the Windows option is provided below.
The instructions assume that you are in the root of the repository.
Type the following commands in Docker "Linux mode" on Windows. The instructions assume that you have a personal Docker user account called mydockername
. You will need to change that to your actual docker account name, such as richlander in the case of the author of this sample. You will also need to create a Docker repo called dotnetapp-prod-arm32
. You can create new repos in the Docker web UI.
You need to be signed into the Docker client to docker push
to Docker Hub.
cd dotnetapp-prod
docker build -t mydockername/dotnetapp-prod-arm32 -f Dockerfile.arm32 .
docker push mydockername/dotnetapp-prod-arm32
Switch to your Raspberry Pi, with Linux and Docker installed. Type the following command.
docker run --rm mydockername/dotnetapp-prod-arm32 Hello .NET Core from Docker
You can build and run the sample locally with the .NET Core 2.0 SDK using the following instructions. The instructions assume that you are in the root of the repository.
cd dotnetapp-prod
dotnet run Hello .NET Core
You can produce an application that is ready to deploy to production locally using the following command.
dotnet publish -c release -o published
You can run the application on Windows using the following command.
dotnet published\dotnetapp.dll
You can run the application on Linux or macOS using the following command.
dotnet published/dotnetapp.dll
Note: The -c release
argument builds the application in release mode (the default is debug mode). See the dotnet run reference for more information on commandline parameters.
The following Docker images are used in this sample