Azure Container Instances

Working with Azure Container Instances (ACI)

Tuba Mansoor
Latest posts by Tuba Mansoor (see all)

Azure Container Instances simply allow you to run containers in Azure. If you are new to containers and dockers, it is recommended that you read my article on dockers and containers before proceeding.

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 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.

See also  Quick and Complete Intro to Artificial Intelligence

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.

Demo

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 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.

See also  ASP.NET Core: The MVC Request Life Cycle
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:

FROM microsoft/dotnet:sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "aspnetapp.dll"]
Azure Container Instances

Goto powershell and navigate to the project directory. Once there run the command:

docker build -t aspnetapp .
Azure Container Instances

Once the project is build 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 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 login 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.

See also  Cloud Computing: Deploy an ASP.NET Core App to PCF?

Running your application as an ACI

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

Azure Container Instances

Create a container instance as below:

Azure Container Instances

Once, 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.