In this article, we will learn how we can create dockerized ASP.Net Core Web API and deploy the API to the Azure Container Registry using Docker. We will see step by step process for the whole process.
Prerequisites
- .Net Core runtime installed on your machine(In the below example we will be working with .Net Core 7 SDK and Runtime)
- Basic knowledge of building applications with Dotnet Core Framework
- Visual Studio 2022
- Docker Desktop installed on your machine
- Microsoft Azure Subscription(Microsoft offers one month free subscription to new users)
Creation of .NET Core Web API
Step 1
Create a new ASP.NET Web API Core Project by following the below path in visual studio.
File -> New -> Project (You will see the new window named Create a New Project, Select the ASP.Net Core Web API as selected below. If you are not able to find the template, you can add API in the search bar)
Step 2
Configure your New Project as per the below screenshot.
Then Click on Next.
Step 3
Please provide all the information in the Additional Information screen. Make sure to check Enable Docker option. However, there are more ways to add Docker support to the application. But in this article, we will do it by selecting it here only.
Then Click on Create button. It will now create your ASP.NET WEB API Core Project. You will see below the structure of your project in Visual Studio.
Creating an Azure Container Registry
The Azure Container Registry created in this step will store the image for API Project. To create a registry, follow the below steps.
Step 1
Login into your Azure portal. Search using the text 'container registries' in the search bar. You will find the Container Registries in the search results. Click on that link, and you will land on the below page:
Step 2
Click on Create button at the top of the page, and you will now land on the new page as per below.
You will get Resource Group and the Registry Name as blank. You can create a new resource group or use an existing one if you have any; for the registry name, you can enter any name. I have added dotnetApiWithDocker name for both the resource group and the registry. Please make sure to remember the name you have entered.
Step 3
You can then click on the Review + Create button to create a registry. It will first check if all the validations are passed. If yes, you will land on the next screen where you can see validation passed, and then you can click the Create button to create a registry. It will take a few mins to create a registry. You will get a notification once it is created.
You will see the below screen once the deployment is completed.
By now, you have created New Registry Instance.
Getting an Access Key from the Registry
Now we need to enable docker access in the Azure Container Registry. This is a crucial step because this is the only step responsible for mapping the docker image with the Azure registry.
To enable docker access, click the Go to Resource button in the last step. Click on the Access Keys option under Settings in the left pane. You will now see the below page.
This screen will display the registry name and the login information. You must enable the Admin User option using the toggle button, as shown below.
The username and password shown on the screen will be required to log in from the docker CLI. Please make sure you keep it in some place.
Overview of Docker file
Remember, while creating an API project, we enable docker support via the checkbox in the additional information screen. That option will create a default docker file in your project and will look like the below.
We will use the same docker file Microsoft provides as default. However, we can customize it as well as per our requirements.
If you are new to DockerFile, the Docker file is the one that has all the required information and command to install all the project dependencies and build and execute them. I will not dive deep into every command used in the docker file; I will explain it in the following article.
Build the Docker Image
Open the Developer PowerShell in Visual Studio, and Run the below command.
In the below build command, you can use your registry URL and other information as we got from the Access keys page
docker build -t dotnetapiwithdocker.azurecr.io/dotnetapiwithdocker:v1 .
You will see the log of every build step in the terminal.
Now you have built the local version of your container image, now let's try to run it using the below command:
Deploying the Docker Image Created to Azure Container Registry
The first step is to log in to the Azure registry and run the below command in Powershell.
docker login -u <username> -p <password> dotnetapiwithdocker.azurecr.io
You would get the message Login Succeded if you provided the correct credentials. (make sure to use the Username and password of Container Registry, not Azure)
Now push the created image to Azure by running the below command.
docker push dotnetapiwithdocker.azurecr.io/dotnetapiwithdocker:v1
The image is now pushed to the repository. You can verify it in Repositories under the Services pane in Azure Portal.
Create a web app service for Container
Now you need to create one web app connected to the container image.
Step 1
Click on Containers -> Web App For Containers to create a new container instance.
Step 2
After clicking on Create button, you will land on the Create Web App information page. You can enter all the details.
You can add any available instance name and use the existing resource we created initially.
Step 3
Now go to the Docker Tab and enter all the details below.
Step 4
Click on the Review + create button, and then click on Create button. You will get a notification once the application is up and running. It will take some time to deploy your application.
Once the application runs, go to the browser and verify the API link. If all goes well, you will see the below response in the browser.
You can stop and run your Container any time you want.
Conclusions
This concludes the tutorial to create .Net Core API and deploy to the Azure Containers and run them as containers with the help of Docker.
I hope this tutorial will help you to understand how an application runs in the Container and how helpful it is nowadays. Please let me know if you have any feedback or suggestions in the comment box.
Thanks, everyone, for taking the time to read this document. Happy coding