Containers, Docker and .NET : Part 1

Siddharth Phatarphod
5 min readDec 2, 2021

--

In this series, we will walk through the basics of the modern approach of containerization of applications and how we can containerize .NET 6 applications in Docker.

We will initially start with the containerization of a very simple .NET 6 console application for our understanding of the steps involved.

What is a container and what are container images?

A container provides runtime environments for various applications so that we can write the application once and port the application between different platforms and clouds.

Containers are dependent on container images to construct a run-time environment and run an application.

Containerized Applications
Containerized Applications

Benefits of containerization:

  1. Containers provide agility to developers to integrate with their existing DevOps environment.
  2. Monolithic applications can be containerized using microservices, so that each microservice has its own life cycle and scaling policies with a dedicated development team.
  3. Containerized applications provide improved security by isolating applications from the host system and from each other.
  4. Containers ensure faster application start-up and easier scaling.

What is Docker?

Docker is an open platform for developers and sysadmins to build, ship and run distributed applications.

Docker consists of the Docker container that has the Docker image files.

Docker images become containers at runtime when they run on docker engine.

Docker Engine is a portable, lightweight runtime and packaging tool.

Docker Containers
Docker Containers

Your development machines will run one Docker host, either Linux or Windows. Related microservices that you want to run and test together in one solution will all need to run on the same container platform.

Why Docker?

Docker containers encapsulate everything an application needs to run , allowing applications to be shuttled easily between environments.

Docker Engine enables containerized applications to run anywhere consistently on any infrastructure, solving “dependency hell” for developers and operations teams, and eliminating the “it works on my laptop!” problem.

Any host with the Docker runtime installed — be it a developer’s laptop or a public cloud instance — can run a Docker container.

Docker enables apps to be quickly assembled from components and eliminates the friction among the development, QA, and production environments.

As a result, IT can ship faster and run the same app, unchanged, on laptops, data center VMs and any cloud.

Decision table: .NET frameworks to use for Docker

Decision table: .NET frameworks to use for Docker
Decision table: .NET frameworks to use for Docker

Containerize a .NET console app in Docker

Let us containerize in Docker the .NET 6 console application detailed in the story https://psid23.medium.com/parking-bill-c-sharp-programming-exercise-from-codility-be5687e1a341

Pre-requisites:
1. Microsoft .NET SDK 6.0
2. Microsoft .NET Runtime 6.0
3. Microsoft Visual Studio 2022 Community Edition(strictly for non-commercial purposes: https://visualstudio.microsoft.com/license-terms/vs2022-ga-community/)

Preferred Desktop/Laptop Configuration:
a. Windows 11 Home Single Language — 64-bit operating system, x64-based processor.
b. Intel(R) Core(TM) i5–10210U CPU @ 1.60GHz 2.11 GHz.
c. 16 GB RAM (15.8 GB usable) — please note Docker Engine will utilize memory from your existing RAM, so the more the RAM the better.
d. 1 TB NVMe M.2 SSD — Docker commands should run without any glitches on an SSD.

The GitHub repository for the project is located at: https://github.com/psid23/ParkingBill/tree/dev-docker

Clone or download the project and open it in Visual Studio 2022 Community Edition.

Build and run the application from Visual Studio or from the dotNet CLI (dotnet publish -c Release), so that it is published with the required deployment artifacts.

Published application
Published application

Now follow the below steps:

  1. Install Windows Subsystem for Linux Update — WSL 2 (https://docs.microsoft.com/en-us/windows/wsl/install-manual)
  2. Install Docker Desktop personal edition(https://www.docker.com/products/docker-desktop) and start the docker engine.
  3. In Command prompt,pull .NET 6 runtime docker image with the below command(if not already present):
    docker pull mcr.microsoft.com/dotnet/runtime:6.0
  4. Create Docker image by running below command:
    docker build -t parkingbillconsole-image -f Dockerfile .
  5. List the docker images to verify the above image got created by using the below command:
    docker images
  6. Create Docker container with the above created docker image by running below command
    docker create — name net6-parkingbillconsole-container parkingbillconsole-image
  7. List the docker containers to verify the above container got created by using the below command
    docker ps
  8. Start the docker container with the below command
    docker start net6-parkingbillconsole-container
  9. Run the docker image with the below command (note the i argument is for interactive mode as the console application requires user input)
    docker run -i parkingbillconsole-image

List of Docker images:

List of Docker images
List of Docker images

List of Docker Containers:

List of Docker Containers
List of Docker Containers

Run Docker image:

Run Docker Image
Run Docker Image

Thanks and enjoy coding!

Will catchup in the next part soon!

References:

--

--

Siddharth Phatarphod
Siddharth Phatarphod

Written by Siddharth Phatarphod

Full Stack | Angular| .Net| .Net Core| SQL | Azure PaaS | Azure DevOps | Git | Scrum

No responses yet