Click here to Skip to main content
15,891,828 members
Articles / Hosted Services / Azure
Article

Working with Azure Container Instances

Rate me:
Please Sign up or sign in to vote.
3.67/5 (2 votes)
8 Apr 2019CPOL3 min read 7.5K   1
How to work with Azure Container instances

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 on production, 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 ACIs, 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 Community Edition.

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. You can find further information here on ACIs.

This article was originally published on my website.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
I have been working in .NET for 8 years now. I like to keep myself updated with new technologies. When I am not coding I like to share my experiences at - https://taagung.com

Comments and Discussions

 
QuestionImages Pin
Sacha Barber14-Apr-19 19:57
Sacha Barber14-Apr-19 19:57 
Your images are unreadable

What you should do is upload 630px wide images to keep code project rules happy

But also upload larger image and make the small image (in article) a link to bigger image

That way when you click on small image you will see a proper readable full size image

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.