How to Deploy ASP.NET Applications in Docker

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

A container is defined as a piece of software that comprises the code and dependencies required for an application to run inside its own environment. As a result, containers are segregated from one another while sharing the same host operating system. The Docker runtime must be installed on the host operating system, which can be Windows or Linux.

This .NET programming tutorial talks about containerization, Docker, and how you can deploy your ASP.NET 6 applications to Docker.

Read: Productivity Tools for .NET Developers

What is Containerization?

Containerization is a technology that creates a virtual environment for applications to run in. When you containerize your application, it runs in its isolated space, with everything it needs to run included and no outside dependencies.

This makes containers lightweight and portable, as they can be moved from machine to machine or between public cloud providers without configuration changes (as long as they support the same OS). Containers allow developers to isolate our applications from one another while still sharing resources with them.

You can think of containerization as analogous to shipping an item: an image is like the box that contains all of your application’s files – when you create a new instance of your application using the image, it becomes an instance that has access to all those files inside.

This means that, if something happens in one instance — as someone breaks into it, programmers do not have to worry about anything wrong happening in other instances because they are isolated from each other.

What is Docker? Why is it needed?

Docker is an open-source container platform that simplifies building, deploying, and managing applications. Docker containers can provide a more portable and lightweight alternative to virtual computers. Containers often encapsulate an application, its runtime, and its dependent libraries.

Docker is a toolkit that allows developers to package your application, its dependencies, and its configuration into a standardized unit. Assuming you have Docker installed, you can then run that unit on your computer. Docker makes it easy to distribute and replicate your applications across multiple environments, such as development, test, staging, and production.

With Docker, a developer’s software applications can be packaged into isolated containers. Using Docker, you can run your applications on all platforms that support Docker. Developing applications in ASP.NET 6 – and then deploying them in Docker containers – can provide programmers with a number of benefits, including being able to work with multiple containers and easily share your applications between development and production environments.

How to Install Docker

You can download Docker for Windows from here: https://docs.docker.com/desktop/windows/install/

Once Docker has been installed on your computer, you can use the following command to find the version of Docker installed:

docker --version

Read: Project Management Software for .NET Developers

Create a new ASP.NET Web API Project in Visual Studio

In this section, we will examine how to build a new ASP.NET Web API project in Visual Studio:

  • Open the Visual Studio IDE
  • Click on “Continue without code.”
  • Navigate to File | New | Project
  • Now select the “ASP.NET Core Web API” project template
  • Click Next to move on to the next screen
  • Specify the name of the project to be created and the location where it should be created in the disk
  • If you would like the solution file to be created in the same directory as the project, select the checkbox that says “Place solution and project in the same directory”; leave it deselected otherwise.
  • Click Next to move on
  • In the “Additional information” screen, specify the target framework you would like to use
  • Specify the authentication type or select “None” if you do not need it.
  • Ensure that you have checked the Enable Docker checkbox as shown in Figure 1

ASP.NET Docker Examples

Further steps:

  • You can deselect the other checkboxes
  • Click Create to complete the process so that a new ASP.NET web application project is created in the path you specified earlier

How to Deploy an ASP.NET Application in Docker

To use Docker to deploy your ASP.NET application, you must first build a Dockerfile. This file specifies how your application will be developed and packaged for use in a Docker container. Using the ‘docker build’ command, you can generate a container image after you have built your Dockerfile. After your image has been created, you may start your container with the ‘docker run’ command.

Edit the Contents of the Dockerfile

Dockerfiles are simple to use and enable easy versioning and customization. They are used to define the process of creating Docker container images. A Dockerfile is a text file that contains a set of instructions for Docker to build an image. It consists of the following sections:

  • FROM – specifies the base image on which you want to build your image
  • MAINTAINER – identifies the person who maintains this repository and/or its contents (optional)
  • COPY – copies files from paths in your local machine into newly created layers of your container at runtime
  • RUN – runs commands inside new layers as they are created with COPY statements or ADD commands

Here is how the Dockerfile looks in Visual Studio:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["DockerDemo.csproj", "."]
RUN dotnet restore "./DockerDemo.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "DockerDemo.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "DockerDemo.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "DockerDemo.dll"]

With Docker Compose, you can create and run multi-container applications. This is useful for situations where you need to run multiple containers, such as when you are running a web application and a database.

Read: How to Work with Serilog in C# and ASP.NET

Command to Deploy the ASP.NET Application in Docker

Once you have built your application successfully, open a command window in administrator mode and execute the following command to create the Docker image:

docker build -t dockerdemo .

And, that is it!

How to Run the Docker Image

To execute the Docker image we created earlier, you can use the following command at the command window:

docker run -d -p 8080:80 --name testapp dockerdemo

Final Thoughts on ASP.NET and Docker

ASP.NET 6 runs on the .NET Core runtime, so it can be executed on multiple platforms, such as macOS, Linux, and Windows. In this programming tutorial, we have examined containerization, its benefits, and how we can deploy an ASP.NET 6 application to Docker.

Read more ASP.NET programming tutorials and software development guides.

Joydip Kanjilal
Joydip Kanjilal
A Microsoft Most Valuable Professional in ASP.NET, Speaker, and Author of several books and articles. More than 25 years of experience in IT with more than 18 years in Microsoft .NET and its related technologies. He was selected as a Community Credit Winner at http://www.community-credit.com several times. He has authored 8 books and more than 500 articles in some of the most reputed sites worldwide including MSDN, Info World, CodeMag, Tech Beacon, Tech Target, Developer, CodeGuru, and more.

More by Author

Must Read