Dockerize Asp.Net 6 Application and Push to Azure Container Registry Its a good practice to put your application into Docker, and with a new version of .NET, it didnt change so drastically. We are going to look at the basic sample provided by the ASP.NET template with enabled Docker. And check what it does and how to improve it.

Introduction

First of all, we need to revise what Docker is and how properly build an image for our application. Docker gives us a way to prepare the isolated environment where we run our application. This environment stays the same for different logic environments such as Testing, QA, Production. Docker introduces some concepts such as Layer, Image, and Container. We need to understand how they work in the scope of this article. Well touch all of them. Its obvious that you should have installed Docker on your local machine. Hands-On Let`s begin with a simple minimal ASP.NET 6 application:

minimalistic ASP.NET 6 API application

The New version of ASP.NET makes it much simpler to create small minimalistic APIs. In the example above server starts and returns “API is UP” when we request the root API path. To verify that the application works, we run it on a local machine with a simple command: [SolutionDir]\DockerExample> dotnet run Now we need to convert such API into a docker image. To do so we will create Dockerfile with instructions on how to build our image:

Dockerfile for ASP.NET API application

In the Dockerfile, our goal is to publish the application and run it. Let`s dive into the image build process. We start from a base image which is SDK version 6.0.100, on top of alpine. I choose that version because alpine images take a smaller amount of space, and we still get good performance for our application run on top of alpine-based images. The next main step is to copy the csproj file and restore all references.

Note: RUN COPY ADD — such instructions create a new image layer that is cached by Docker, and during the next image build, caching speeds up the process significantly. But be aware that the number of layers increases result in image size.

When the restore step is finished successfully, we copy all files in our solution and run a publish command with the required configuration for release. At this step no need for additional restoration because we finished it in the previous step.

Now for us, its better to start from a new image and copy only the required assets to this image. Again we choose the alpine-based image, then copy artifacts from previous steps to a new image and run the command to start an application. We are ready to build an image. We have everything that we need: a working project and Dockerfile with instructions: <[SolutionDir]> docker build -f DockerExample/Dockerfile -t example:1 . --no-cache`

Run the following command, and Docker generates a new image on your local machine. Dont worry; Docker is clever enough to understand that some images are missing on your machine, and itll download them automatically.

When it is completed, you can verify a new image on your machine: docker image ls Check that image is present in the list of local docker images: docker run -d --p 8080:80 --name example-app example:1 It starts a container on your machine using the image which weve just created and binds port 8080 to local 80 inside the image. Open a browser and try to request the next page http://localhost:8080/` it returns API is UP

How to publish a local image to the Azure container registry In addition, I want to show a way how to push a local image to Azure Container Registry. Its useful if you want to run it on Azure Kubernetes Service, for example. I assume that you have an account in Azure; if not, you can signup. All resources which will be created in this article should belong to a resource group, and its better to create a new group for such a purpose.

Open Azure Portal, navigate to Resource Group and click on the plus to create a new resource group.

create a new resource group It`s enough to provide only the resource group name, and you can press the button Review+Create. The Next step is to navigate to Container registries and create a new registry which is bound to just created resource group.

create a new container registry To proceed further, you should install azure-CLI execute commands in order to push a local image to the container registry: az login after success login into your account, also you should log in to your container registry with the next command: az acr login --name examplemediumrepo As you remember, we`ve created an image and tagged it with example:1 docker tag example:1 examplemediumrepo.azurecr.io/exmaple:1 now we can push the image to the container registry with the next command: docker push examplemediumrepo.azurecr.io/exmaple:1

container registry and image push You will find in the repositories section information about your images. As you see in the image, our example was successfully pushed to the azure repository.

Conclusion The process of creating Docker images for ASP.NET 6 stays the same as it was for ASP.NET 5 and previous .NET CORE versions. We should update images in our Dockerfile and follow best practices during image building. Azure Container Registry offers a perfect solution to host your images in private repositories. But be aware that the selected plan for container registry offers a limited amount of space for images. If you exceed it, you will be charged additionally regarding the azure price list. Its better to make images as small as possible and prepare a cleaning mechanist or do it manually if you dont want to pay for additional space.