Containerize .NET Core Application And Push It To Azure Container Registry Using Docker Commands

Introduction

Azure Container Registry is a private container registry on the Azure Platform. You can containerize your application and push it to the Azure Container Registry. Container-based services like Azure Container Instance, Azure WebApp, Azure Kubernetes Service and Azure Service Fabric can pull the image and run it. You can also pull the image from anywhere outside the Azure platform like on-premises or other vendor clouds and run it. The images stored in Azure Container Registry are not publicly available and you must give explicit permission to users who should be pulling and using it.

You can create a .NET Core application, containerize it and push it to Azure Container Registry using Visual Studio. However, all applications are not developed using Microsoft Technologies and Azure Container Registry can keep containers images for applications built on non-Microsoft technologies like Java, Python, etc. In such cases you can use Docker commands to containerize the application and push it to the Azure Container Registry.

In this article we will containerize a.NET Core application and push it to the Azure Container Registry. The folowing are the links to the previous articles in this series.

Prerequisites

You should have these following prerequisites ready before trying out the steps illustrated in this article.

  • You should have created an Azure Container Registry
  • You should have created a Dockerfile to containerize the image
  • You should have kept the Dockerfile in the same folder where you solution (.sln) file is present for the .NET Core application
  • You should have installed Docker Desktop on your system

Containerize .NET Core application

Once you have developed your application and met the above-mentioned prerequisites, open the command prompt or PowerShell prompt and navigate to the folder where you have kept the Dockerfile. Run the following command to build the Docker image for the application.

docker build -t mydemoapp:demo .

Now let us verify if the image got created using the following command.

docker image ls  

Abhishekmishra

You can run the docker image locally as a container using the following command. You are publishing the application to the port 80 in the container and mapping it to local port 8080 on your system. 

docker run --publish 8080:80 --detach --name mydemoapp mydemoapp:demo  

You can access the containerized application using the following command.

http://localhost:8080

Containerized application

Push the container to the Azure Container registry

Now let us go to the Azure Container Registry in the Azure Portal. Navigate to the Access Keys and enable Admin User. Copy the Username and Password.

Access keys

Run the following command to authenticate with the Azure Container Registry. Provide the username and password for the Azure Container Registry when prompted. 

docker login acrfordemo.azurecr.io  

Run the following command to tag the Docker image to the Azure Container Registry.

docker tag mydemoapp:demo acrfordemo.azurecr.io/mydemoapp:demo  

Run the following command to push the image to the Azure Container Registry.

docker push acrfordemo.azurecr.io/mydemoapp:demo  

Go to the Azure Portal and open the Azure Container Registry. Go to Repositories and verify if the image is successfully pushed.

Demoapp

Conclusion

In this article, we learned how to containerize a .NET Core web application and push it to Azure Container Registry using Docker commands. You can use this technique to containerize and push container images for non-Microsoft applications to the Azure Container Registry.