Building a Docker Image for a .NET Core application

Building a Docker Image for a .NET Core application

Hi all,

Since the past few years, industries have started to moving their application workloads rapidly to the Cloud(AWS/Azure/GCP..) as compared to traditional on-prem infrastructure. One major benefit that these cloud providers offer is to rapidly scale in/out the applications based on the incoming traffic ensuring optimum balance is reached between maintaining the utilization vs capacity debate!

Before containers came into existence, traditionally the applications were deployed on virtual servers such as EC2 or VMs. These VMs virtualize the physical host and we can create multiple VMs on top of a single physical host. The problem was scaling in/out took a lot of time since spinning up/down the servers takes a lot of time.

The biggest benefit that container offered was - It virtualized the operating system. Meaning, if we have multiple enterprise applications that are using a common operating system(Linux or Windows) and a common runtime(Such as .NET Framework 4.7 or .NET Core 3.1), then we can have multiple containers live on a single VM that will share the underlying OS and below!

To quote the definition of "What is a container?"

A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

Container images become containers at runtime and in the case of Docker containers – images become containers when they run on Docker Engine. Available for both Linux and Windows-based applications, containerized software will always run the same, regardless of the infrastructure. Containers isolate software from its environment and ensure that it works uniformly despite differences for instance between development and staging.

Now that we know the benefits of a container, let's look at how to create a docker image for a sample .NET Core application.

Pre-Requisites:

  1. Download and Install Docker Desktop
  2. Download and install Visual Studio Code

Overall, let's divide the activity in three parts:

  • Creating a sample .NET Core application
  • Creating a docker file
  • Creating a docker image out of the docker file

Let's Go!

Creating a sample .NET Core MVC application

  1. Create a working directory 1.jpg
  2. Create a template project using dotnet CLI command "dotnet new mvc" 2.jpg
  3. Build the project using "dotnet build" 3.jpg

Now, that the application is ready, let's look at how to create the docker file for the application using Visual Studio Code:

  1. Go to VS Code and open the folder 4.jpg
  2. Select "> Add Docker Files to Workspace" 5.jpg
  3. Select Application Platform(In our case ASP.NET Core) 6.jpg
  4. Select target Operating system which we'll use to run the docker container 7.jpg
  5. Select Port Mapping 8.jpg
  6. Voila - The docker file is ready 9.jpg

Let's look at how to build an image out of the docker file:

  1. Create a docker image from the docker file using "docker build" command 10.jpg
  2. Verify docker image generated locally using "docker images" command 11.jpg
  3. We can even view the docker image on the Docker desktop application 12.jpg

What next?

Since we have the docker image ready, we can push this docker images to any of the registries such as:

  1. Docker Hub
  2. AWS Elastic Container Registry(ECR)
  3. Azure Container Registry(ACR)

and deploy the containerized application on the cloud using:

  1. AWS Fargate
  2. Azure Container Instance
  3. Azure App Service

Hope you found it valuable!