How to deploy a .NET CORE application on a Linux Container hosted on Azure? (Mar 19, 2017)


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:

mkdir azureLinuxContainerSample

Then go inside this folder:

Cd azureContainerSample

Now using CLI run below:

dotnet new

 

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:

 

FROM microsoft/dotnet:latest

WORKDIR /app

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

dotnet restore

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 .

docker build

 

 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.

docker images

 

docker build

 

Now let’s run this image by running it.

docker run azureLinuxContainerSample

docker build

 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

docker build

 We also need to tag our image as follow:

Docker tag azurelinuxcontainersample  azurepropertymanagement-on.azurecr.io/azurelinuxcontainersample

docker build

 And now it is the time to push the image:

Docker push azurepropertymanagement-on.azurecr.io/azurelinuxcontainersample

docker build

 It takes some time to upload the image to azure container registry and after completion we will see below:

docker build

 Now we can see the image in azure container registry as below:

docker build

 

Now let’s create a Linux box on Azure:

docker build

 

Click on Docker on Ubuntu Server option:

docker build

 

Enter Host Name, User name, password.

docker build

 

 Click on Pricing Tier and to see available options:

docker build

 

Select the best option based on your requirement:

docker build

 

 Click on Create button.

docker build

 

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:

docker build

 

On Putty:

docker build

 Enter your IP address of the host from Azure portal and Click on Open:

docker build

 Enter your password:

docker build

 

Here let’s get the docker version:

docker build

 Let’s see if we have an image in this machine:

docker build

 

Let’ pull our image:

docker build

 

So we need to login:

docker build

 

Now let’s pull the image:

docker build

 

It started to pull the image and its dependencies and it was quick and fast.

docker build

 

Docker images:

docker build

 

Let’s run it:

docker build

 

 

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.

 

Conclusion:

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.

Back to All Articles

Number of Views:628