ASP.NET Core and Docker

Last Updated on Nov 15, 2024

“Docker” is a buzzword nowadays, and almost everyone has heard about it a lot. Most of you might know Docker superficially. Like its Open-source containerization technology based on Linux which enables developers to create and deploy programs using containers. But it is vital to have a deep idea about Docker, what is it? What are Docker images and how do you use them in the net core application? How to use the docker run command in our ASP.NET core applications?

1. Can I Use it with ASP.NET Core?

In this blog, we are going to explore how we can use docker with dot net core applications. To start with, let us delve deeper.

Docker container is used for developing and publishing applications. It is a Linux-based open-source platform. It is a way of hiding or protecting your code which you need to run applications, ASP.NET core is one of them.

It is dependent on the type of operating system you use. You can put the docker build command in the docker container to protect it from breaching. In docker, you can neatly put everything into one package. To build that package without repeating the same process again, Because you can copy it anywhere either in your development or production and can reuse it as many times as you want.

Containers are used to deploy applications and this is called containerization. They are used to easily deploy applications.

Containerization is gaining a lot of popularity because they are:Lightweight, Portable, Flexible, Scalable,Loosely coupled, and still Secure

2. Containers and Virtual Machines

You might resemble it a lot with a VM- Virtual Machines but this is different from the VM?

As you can see in the below image, the docker images execute natively on Windows or Linux. The container will be shared with other containers and the kernel of the host machine. It can also run as a separate process which will be faster as it consumes less memory than others.

Whereas a Virtual Machine executes on a completely guest operating system, it requires virtual access to host with software. This will create and run the virtual machine like Hypervisor. In short, a Virtual Machine takes a lot of loads compared to others. Moreover, it eats up your application logic.

3. Docker Tooling in Visual Studio

Docker tooling in visual studio IDE can help us to create and add Docker support to the project. This also generates the correct docker file. You can modify this file and also the container and can run it on the visual studio code running section.

At the time of project creation, ensure whether the “Enable Docker Support” option is checked in, this will allow you to enable docker support for your project.

You can also add the Dockerfile in your project folder later by using “Adding Docker Support” as shown in the below screenshot.

It will ask the option for which OS you want to target to run the Docker. Here you can select Linux from the given options available for docker commands.

Later,  it will generate the Dockerfile of your application. This file is like commands which are like a stepwise instruction on how to build up your docker image.

This is a multi-stage docker file. This means you can make use of any base image and can modify it. Then you can use the created container to build another container image. One of its examples is that the container image contains the .Net Core SDK which is good especially on Linux. This allows you to build an application within a container but there can be cases when you do not want extra SDK for your final deployment image. This is because you want to reduce the runtime of core apps. You want a really small, lean, and fast product. So you can see that the last block starts with the base as the final image.

You need to make sure that you build a container image that has an ASP.NET core SDK. It has got all the packages that you need. Through these multiple layers that it has labeled in the first section as the base and then in that last section down by using it as a first set which is not actually going out to another registry to pull another one.

I’m utilizing the one that was designed to function as an intermediate and is perfectly positioned on the right side. We can use it again from the publisher which is building a released version of the web application. Now put all into the publish layer and then combine all of it again that will assemble everything at the very end of the entry point. So, here we are running an application from visual studio.

As you can see in the below image, select the new option Docker when you add docker support that gets added automatically to run this Dockerfile.

Voila !! 
We get our hello world

As mentioned above Visual Studio, also supports debugging by putting the breakpoint.

4. Deploy a .NET Core App to Docker Hub Using Visual Studio and Run it in Azure App Service

Here are the steps to deploy a .NET Core app to Docker Hub using Visual Studio and run it in Azure App.

4.1 Prerequisite

First, we’ll have to install Docker for Windows before we create a Docker container.  This is available for Mac and Linux. You can also download and run the installation file as you do. You just need to login with a Docker hub account. You are required to create an account.

During the installation, it will ask you different options to choose Windows/Linux Containers. We’ll select the option for Windows Containers. By the way, this can also be changed later in the Docker settings.

4.2 Containerize an ASP.NET Core Application and Host the Image in Docker Hub

Let’s create a new project. For that we will go to File->New Project->Web-> ASP.NET Core web application

We can select the checkbox “Enable Docker Support” or you can add the docker file later in the project

As shown in the below image, we can now have a Docker file in the project. This file contains the container configuration. It uses operating system images to build the container and run the application on it.

 Click on the project and select Publish. Now, we will choose Container Registry and  Docker Hub, as we want to put the Container on Docker Hub.

Here, fill in your DockerHub username and password. Your username is not an e-mail address. You’ll see your username when you sign in to the Docker Hub website.

That’s it. Let’s publish it.

We are now logged in to the Docker Hub website. This is a container image that we’ve just published from Visual Studio.

Here, we can see that Visual Studio has added a tag that says latest to the image. You can use tags for identification and versioning.

4.3 Use Azure App Service Web Apps for Containers to Run the Container Image

So, let’s run this Container. I’ll switch to the Azure portal. From here, I’ll create a new web app for Containers. This is a web app like the Standard App Services web app that runs Containers instead of running an application directly.

Adding container support to an ASP.NET Core application is really simple from Visual Studio. From there, you can easily publish it to Docker Hub or any other Container Registry and run it in Azure.

Let’s create one. Let us fill in all the information here. First, we need to fill in a name and resource group.

Now, for the container. I can choose where the Container comes from. I will choose Docker Hub as that is where my Container is right now. so then I will insert all the details in the container name and also need to add the tag which is the latest.

This is the web app for containers, and it is running on a container with the ASP.NET Core application in it. Now let’s go and check the URL and see if it works.

Yes. There it is, running within the container .

5. Conclusion

In this long discussion of how to use docker with the integration of .NET, we have tried to discuss all the important aspects of developing an ASP.NET Core application using Docker in this process. The advantage of Dockers is that it is more flexible and light than Virtual Machines and provides therefore a minimal resource and overhead usage. This makes it inexpensive because one Virtual Machine would suffice to run several Docker containers. And the Docker Hub provides many pre-built images and tools that you can use for your own customized solutions.

Comments


Your comment is awaiting moderation.