Multi Stage ASP.NET Core 2.0 Web App Build with Windows Containers And Docker

In this blog post, I’ll show you how I build an ASP.NET Core 2.0 Web Application using multi-stage build and make my application more secure, light and efficient.

About Multi-Stage Build

Multi-stage build allows me to use two different Container Images to build .NET applications by using one Container for building the code using tools (SDK) and another Container just to run the code (runtime).

In my case, I’ll use one Container for the build which has the .NET Core 2.0 SDK tools loaded and another Container to just run the app with runtime tools Installed only.

Both Images runs on Windows Nano Server 2016, however, the size of the SDK Image Is almost double the size of the runtime Image.

Why Using Multi-Stage Build?

The first reason Is size, the SDK Image comes with many tools and options needed for building, debugging, restoring, etc applications that are not necessary for running the app.

Efficiency, In large microservers environment where each service runs on a container the build process needs to efficient, light and small.

Security, Smaller Images need less downtime, patching and updates which make them more secure.

Web App

In the example below, I’ll create an ASP.NET Core 2.0 Razor web application on the full dotnet container Image, once completed I’ll spin another Container with the ASP.NET Core 2.0 Image which Is half the size of the dotnet Image to just run the code.

All the Web App data will be saved to a Volume on the Container Host.

Get Started

To get started, I’ll download the full Dotnet Image with the SDK loaded.

docker pull microsoft/dotnet

Below, I’ll download the ASP.NET Core 2.0 Container Image which Is almost 50% smaller than the Dotnet app but only has the runtime tools Installed

Docker pull microsoft/aspnetcore

docker images

Below, you can see the two Images and their size on disk after download.

The dotnet Image Is 2.6 compared to only 1.3GB for the ASP Image.

Build Web App

To build by Web App on the dotnet Container, I’ll run the command below and will use a Volume on my host to store it on and I’ll use later to run the Web App

docker container run --name webappbuild -it -v c:\dotnetbuild:c:\data microsoft/dotnet

Inside the Container, I’ll run the lines below that will create, build, and publish my Web App.

Mkdir app01
Cd app01
Dotnet new razor
Dotnet build
Dotnet run
Dotnet publish


Now, All I need to do Is to run my Web App on the ASP container Image using the code below with the same volume.

Below, I have types of codes, One with port mapping and the second without, to see that it works I’ll use the second line.

docker container run --name webapp -it -v c:\dotnetbuild:c:\data microsoft/aspnetcore
docker container run --name webapp -p 8000:80 -it -v c:\dotnetbuild:c:\data microsoft/aspnetcore

From Inside the container Web App mapped volume, I’ll run the code below to run the Web App.

cd c:\data\app01\bin\debug\netcoreapp2.0\publish
dotnet app01.dll

And using port mapping I can see my Web App In action

Leave a Reply

Your email address will not be published. Required fields are marked *