How To Containerize Your ASP.NET Core Application

Docker scaled 1

IT Tips & Insights: A Softensity engineer walks you through how to use Docker to containerize an ASP.NET core application.

By Helder Borges, Software Engineer

What is Docker?

Docker is an open-source project for automating the deployment of applications as portable, self-sufficient containers that can run on the cloud or on-premise. Docker is also a company that promotes and evolves this technology, working in collaboration with cloud, Linux, and Windows vendors, including Microsoft.

Virtual Machines & Containers

Virtual machines include the application, the required libraries or binaries, and a full guest operating system. Full virtualization requires more resources than containerization.

Containers include the application and all its dependencies. However, they share the OS kernel with other containers, running as isolated processes in user space on the host operating system. (Except in Hyper-V containers, where each container runs inside of a special virtual machine per container.)

What is Containerization?

Containerization is an approach to software development in which an application or service, its dependencies, and its configuration (abstracted as deployment manifest files) are packaged together as a container image. The containerized application can be tested as a unit and deployed as a container image instance to the host operating system.

Just as shipping containers allow goods to be transported by ship, train, or truck regardless of the cargo inside, software containers act as a standard unit of software deployment that can contain different code and dependencies. Containerizing software this way enables developers and IT professionals to deploy them across environments with little or no modification.

Docker terminology:

Container image
A package with all the dependencies and information needed to create a container. An image includes all the dependencies (such as frameworks) plus deployment and execution configuration to be used by a container runtime. Usually, an image derives from multiple base images that are layers stacked on top of each other to form the container’s filesystem. An image is immutable once it has been created.

Dockerfile
A text file that contains instructions for building a Docker image. It’s like a batch script — the first line states the base image to begin with and then follows the instructions to install required programs, copy files, and so on, until you get the working environment you need.

Docker container
A lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

ASP.NET Core 6 Dockerfile

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base

WORKDIR /app

EXPOSE 80

EXPOSE 443

 

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build

WORKDIR /src

COPY [“MyApp.API/ MyApp.API.csproj”, “MyApp.API/”]

RUN dotnet restore “MyApp.API/MyApp.API.csproj”

COPY . .

WORKDIR “/src/MyApp.API”

RUN dotnet build “MyApp.API.csproj” -c Release -o /app/build

 

FROM build AS publish

RUN dotnet publish “MyApp.API.csproj” -c Release -o /app/publish

 

FROM base AS final

WORKDIR /app

COPY –from=publish /app/publish .

ENTRYPOINT [“dotnet”, “MyApp.API.dll”]

 

Build & Run 

Make sure that you have installed Docker on your machine, then open a terminal and go to the app directory with the Dockerfile. Now build the container image using the docker build command: 

docker build -t myapp .

This command used the Dockerfile to build a new container image. 

After the Microsoft image was downloaded, we copied in our application and used dotnet to restore our application’s dependencies. The ENTRYPOINT directive specifies the default command to run when starting a container from this image.

Finally, the -t flag tags our image. Think of this simply as a human-readable name for the final image. Since we named the image myapp, we can refer to that image when we run a container.

The . at the end of the docker build command tells Docker that it should look for the Dockerfile in the current directory.

Start your container using the docker run command and specify the name of the image we just created:

docker run -dp 80:80 myapp

We’re running the new container in “detached” mode (in the background) and creating a mapping between the host’s port 80 to the container’s port 80. Without the port mapping, we wouldn’t be able to access the application.

BIO

Hi there! I’m Helder, a software engineer with almost a decade of experience. Most of my experience is .NET stack but I have also worked with Ruby on Rails and Nodejs. I have experience working for different segments like e-commerce and tax system calculations. I’m also certified by CNCF, both CKA (Certified Kubernetes Administrator) and CKAD (Certified Kubernetes Application Developer).

Join Softensity’s Team