NashTech Insights

Docker & ASP.NET Core: Part 1 – Overview

Hai Hoang
Hai Hoang
Table of Contents

Containers have surged in popularity since Docker’s introduction in 2013. A lot of companies have already incorporated containers into their operational processes, streamlining the deployment, distribution, management, and scalability of their software. Within this article, I will provide insight into containers and the concept of containerization. Furthermore, I will also explain the difference between containers and virtual machines. I will also show you how to install Docker on Windows and how to “dockerize” both a new and an existing ASP.NET Core application.

Overview of Containers and Containerization

A container is a portable and self-contained unit of software that includes everything needed to run an application, including the code, dependencies, runtime, tools, settings, and libraries. Using containers, the developers can ensure that their code runs reliably and consistently, regardless of the underlying infrastructure.

Containerization is a software deployment process that bundles an application’s code with all the files and libraries it needs to run on any infrastructure. Traditionally, to run any application on your computer, you had to install the version that matched your machine’s operating system.

What is Docker?

Docker is an open-source container management system that enables developers to develop, run, test, and deploy their applications in lightweight virtualized environments called containers. These containers include all required dependencies, tools, libraries, and configuration settings, making it easy to package and distribute your application for different environments and operation systems consistently.

What is Docker Image?

Docker image is a self-contained, read-only file that acts as a template or blueprint for creating Docker containers. It includes a lightweight operating system, application code, runtime libraries, system tools, and any other dependencies required to run a particular software application. Docker images are built using a layered approach, with each layer representing a specific set of changes or additions to the base image. These images can be shared, distributed, and deployed across various systems and platforms, providing a consistent and reliable environment for running applications.

What is Dockerfile?

A Dockerfile is a text file that contains a set of instructions and commands for building a Docker image. It serves as a recipe for creating customizable Docker images. A Dockerfile allows a developer to automate the process of building his Docker images, ensuring consistency and easy deployment across different environments. The Dockerfile contains commands for defining the base image, adding dependencies, copying files to the image, setting environment variables, running commands, exposing ports, and configuring other aspects of the image. Each command in the Dockerfile represents a layer in the resulting image, enabling efficient caching and incremental builds.

Docker Containers vs Virtual Machines

CategoryDocker ContainersVirtual Machines
ArchitectureShared Host Operating System
Docker containers share the host operating system kernel. They are suited for situations where you want to run multiple applications over a single operating system kernel.
Separate Operating System
Virtual machines run on a hypervisor that emulates an entire operating system, including its kernel. They are suited for applications or servers that need to run on different operating system flavors
Resource UtilizationLess Resource Usage
Containers are more lightweight than VMs because they do not require a separate guest operating system. Multiple containers can run on a single host, sharing the host’s resources efficiently.
More Resource Usage
Virtual machines have their own guest OS, which consumes additional resources and limits the number of VMs that can be run on a given host.
Isolation and SecurityProcess Level Isolation
Containers provide process-level isolation, meaning each container is isolated from others on the same host.
Fully Isolated
Virtual machines offer stronger isolation by providing full isolation at the OS level. This makes VMs more suitable for running applications that require strict security or incompatible software requirements.
PortabilityHighly Portable
Docker containers are highly portable since they encapsulate all the necessary dependencies within the container image. Containers can easily be moved across different environments, hosts, and cloud platforms.
Less Portable
Virtual machines require a hypervisor and an operating system to be installed, making them less portable and requiring more configuration when moving between environments.
Boot timeFast boot time
Containers can be instantiated in seconds.
Slow boot time
VMs can take several minutes to instantiate.

Docker Support in ASP.NET Core Projects

  1. For building, debugging, and running containerized ASP.NET Core applications for .NET Core, you have to use Visual Studio 2017 or later versions.
  2. To containerize an ASP.NET Core project, the project must target .NET Core.
  3. Both Windows or Linux containers are supported for ASP.NET Core applications
  4. If you want to add Docker support to your project using either Windows or Linux containers, make sure that a container of the same type must be running on the Docker host.
  5. Visual Studio Container Tools does not support adding Docker to existing ASP.NET Core projects targeting the .NET Framework. 
  6. Docker support can be added to both new and existing ASP.NET Core projects in Visual Studio.
  7. You can debug your ASP.NET Core application running inside a Docker container using Visual Studio or Visual Studio Code.
  8. A Dockerfile that contains instructions to build a Docker image for your application can be added in ASP.NET Core applications in which you specify the base image, copy the necessary files, and configure the container environment.

To be continued…

Hai Hoang

Hai Hoang

Engineering Manager at NashTech VN

Suggested Article

%d bloggers like this: