In Focus

Working With Azure Container Instances

Azure Container Instances simply allow you to run containers in Azure. The advantage is you can run it for a particular amount of time and then stop the container. You will just be billed for the time your container runs.

Introduction

Let’s have a brief recap before proceeding. Suppose, we as developers have created an application. When we create a container image for our application, we pack up our code, as well as all its dependencies and tools needed to run it as a container. The advantage is that when we run this image as a container, it will run the same way as it did on our development machine. This is because the application will use the same set of resources irrespective of which machine we run it as a container on. When we run an application as a container, it will run the same way everywhere. Docker is a tool that helps us deal with creating and running containers. Again if you still have any doubts regarding containers touch base with my article here. Also in my earlier article, we created a virtual machine on Azure and deployed our container there. You can also run containers on Azure as ACI’s (Azure Container Instances). Let’s learn how.

What are Azure Container Instances?

Using ACI’s, you can simply run your containers on Azure. The advantage is you can run it for a particular amount of time and then stop the container. You will just be billed for the time your container runs. It’s really easy to use.

Creating a virtual machine

ACI works better with images created with Windows 2016 systems rather than with Windows 10 systems. So, I created a Windows 2016 virtual machine on Azure.

Azure Container Instances

When you create a virtual machine, be careful of the disk size that you choose. Not all disk sizes support double virtualization. Also, you need to allow all RDP connections to be able to connect to Azure.

Azure Container Instances 

Once the virtual machine is created, we would want to install Visual Studio 2017 and Docker for Windows Community Edition next. But the thing is that servers have very strict security policies, especially when you try to browse through Internet Explorer. So, I downloaded Chrome.exe on to my actual system and then copied it to my virtual machine. I then installed Chrome on my virtual machine and later, using Chrome, installed Visual Studio 2017 & Docker for Windows.

Once all the installations are done, let’s create an application as below.

Creating an application

Create a new ASP.NET MVC core project in Visual Studio 2017. While creating make sure you have the ‘Enable Docker Support’ option checked.

Azure Container Instances 

I call my app ‘aspnetapp’. Once you enable docker support, a file called dockerfile is created in the solution explorer.

Azure Container Instances

Replace the existing code in this file with the following piece of code.

  1. FROM microsoft/dotnet:sdk AS build-env  
  2. WORKDIR /app  
  3.   
  4. # Copy csproj and restore as distinct layers  
  5. COPY *.csproj ./  
  6. RUN dotnet restore  
  7.   
  8. # Copy everything else and build  
  9. COPY . ./  
  10. RUN dotnet publish -c Release -o out  
  11.   
  12. # Build runtime image  
  13. FROM microsoft/dotnet:aspnetcore-runtime  
  14. WORKDIR /app  
  15. COPY --from=build-env /app/out .  
  16. ENTRYPOINT ["dotnet""aspnetapp.dll"]  
Azure Container Instances
 
Go to PowerShell and navigate to the project directory. Once there, run the command.

docker build -t aspnetapp .

Azure Container Instances

Once the project is built, run the following command.

docker run -d -p 8080:80 --name myaspnetapp aspnetapp

Azure Container Instances

Once this is successful, go to localhost:8080 to navigate the app.

Azure Container Instances

So, this is how we run the app in a container. Now, let’s check out how we can run this container as an ACI.

Creating an ACI

Let’s create a container registry in Azure as follows.

Azure Container Instances

You can find your username and password in the Access Keys section as shown below. We will need these when we push our app to the container registry.

Azure Container Instances

We will also create a repository called ‘demo’.

Azure Container Instances

Open PowerShell and type in the following code to log in to your container registry with the access keys, as discussed above.

docker login yourContainerRegistryLoginServer

Azure Container Instances

docker tag yourImageName yourLoginServer/yourRegistryName:yourTag

docker push yourLoginServer/yourRegistryName:yourTag

Azure Container Instances

This pushes your application to Azure.

Running your application as an ACI

Open your container registry in Azure portal, go to the Repository tab, and click on ‘Run instance’.

Azure Container Instances

Create a container instance as below.

Azure Container Instances

Once the deployment is successful, you will be notified as below.

Azure Container Instances

Now, go to your container instance as below.

Azure Container Instances
 
You will find the IP address as below.
 
Azure Container Instances

Navigate to the IP address to run your container.

Azure Container Instances

Your app will now run as a container instance. You can stop and run your container any time you want.

This concludes the Azure Container Instance tutorial. Find further information here on ACIs.