How to deploy a .NET CORE application on a Linux Container hosted on Azure?
In previous article, we saw the benefits of Azure Container Registry and also how to create an azure container registry in azure portal and how to push a docker image to it.
In this article, I am going to walk you through how to deploy your .NET CORE application on a Linux container on Azure portal. In order to do this we first create a simple .NET CORE application using command line interface and then build a docker image from this application. Then we will push this image to our azure container registry. After that we will see how to create an Ubuntu VM in Azure portal and how to connect to it using a tool named putty. Finally, we see how to pull the image from azure container registry and run our .NET CORE simple application on a Linux container hosted on Azure.
So let’s get started. First we need to create a simple .NET CORE application. We will create a simple Hello World app for the simplicity as we are not going to explore .NET CORE development here.
On your development machine Open a new command prompt and create a directory e.g. azureLinuxContainerSample as follow:
Then go inside this folder:
Now using CLI run below:
It created a new .NET CORE application that outputs hello world to the console. Next step is to add a docker file to this project. Here is the content of the docker file:
COPY /app /app
ENTRYPOINT dotnet azureLinuxContainerSample.dll
So ‘From’ would say from this image. Then we would say inside the container set the working directory to /app folder. Then copy from /app folder from host machine to the app folder of the container.
After that we say what the entry point is when the container starts. So I would say ENTRYPOINT dotnet azureLinuxContainerSample.dll Now we need to run dotnet restore in powershell to restore all required packages
Then we build it:
dotnet build –c release
Now notice that we have a new bin folder. Now, I would say ‘dotnet publish –c release –o app’ to create app folder and its content.
dotnet publish –c release –o app
And it creates the app directory. Now that we have our app built, we need to build docker image. In order to build an image we need to tag it with –t and also add a dot at the end to look for dockerfile in the current context:
Docker build –t azurelinuxcontainersample .
Please note that docker build command requires that the tag should be all lower case. Now we have built our images and if we do a docker images we will have this images in the list.
Now let’s run this image by running it.
docker run azureLinuxContainerSample
Okay, so we are in a windows machine, we just compiled this .net application. Then we have built an image. Then we ran it inside a Linux container on my windows development machine. In addition to that we can put all other required dependencies for our application in the docker file and have the environment ready to run our application.
For pushing the image to azure container registry, we need to login first to azure container registry:
Docker login yourLoginServer –u yourUserName –p yourPassword
We also need to tag our image as follow:
Docker tag azurelinuxcontainersample azurepropertymanagement-on.azurecr.io/azurelinuxcontainersample
And now it is the time to push the image:
Docker push azurepropertymanagement-on.azurecr.io/azurelinuxcontainersample
It takes some time to upload the image to azure container registry and after completion we will see below:
Now we can see the image in azure container registry as below:
Now let’s create a Linux box on Azure:
Click on Docker on Ubuntu Server option:
Enter Host Name, User name, password.
Click on Pricing Tier and to see available options:
Select the best option based on your requirement:
Click on Create button.
Now that we deployed our Azure Linux VM, we need to connect to it using putty.
IP address of the machine is available in Azure portal:
Enter your IP address of the host from Azure portal and Click on Open:
Enter your password:
Here let’s get the docker version:
Let’s see if we have an image in this machine:
Let’ pull our image:
So we need to login:
Now let’s pull the image:
It started to pull the image and its dependencies and it was quick and fast.
Let’s run it:
And here is the Hello World! Output on the terminal which shows that the application is successfully deployed on a Linux container hosted on Azure.
In this article, we explored how to build a docker image, how to push it to azure container registry, how to provision a linux server on azure, how to pull image from azure container registry to this linux machine, and how to run a docker image on a linux machine hosted on azure.
Number of Views:1620