Using Docker with Azure Container Instances speeds up development without the need to manage the infrastructure that runs it.
In this article we are going to create a .Net Core Web API, create an image of it using Docker and host it using Azure Container Instances.
With the slogan “Developers Love Docker, Businesses Trust It” we can understand why Docker is so popular. The developer proverb, “It works on my machine.” is no longer a problem because we can deploy our “own machine” using Docker. Docker allows us to create images with our apps to deploy to containers.
Why Azure Container Instances?
It is a Microsoft cloud-based host for Docker images. You don’t need to manage virtual machines or learn new tools—it’s just your application, in a container, running in the cloud.
How-to Host your Docker Containers in Azure Container Instances
We’re now going to run through the steps of how to create a .Net Core Web API, then an image of it using Docker and how to host it using Azure Container Instances.
Creating your Web API
Using Visual Studio, create a new Web API project called SimpleApi.
Tick the option “Enable Docker”, it will create the Dockerfile file with all configs needed to create a new Docker image with our API inside.
Creating the Docker Image
Now you have your API running you can create a Docker image. For this task, you will need to have the Docker desktop running on your local machine. You can download it from the Docker website https://www.docker.com/products/docker-desktop/. Now with Docker running on your machine, you need to open the command prompt, navigate to the root of your project folder and execute the following command:
->docker build -t simpleapi.
it will create a docker image named “simpleapi”
Creating the Azure Container Registry
Go to Microsoft Azure Portal, create a new resource and look for “Azure Container Registry”.
Create it and follow the steps according to the images, I have created mine with the name: “mycontainerstest”.
Pushing the Docker Image to Azure Container Registry
Now we have our image created and the Azure Container Registry created we can push the image to the registry.
We will use the command prompts for this task. You have to install the Azure CLI on your machine to allow the use of the Azure commands in the prompt, if you don’t have it already, you can follow this article: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli. After installation, you can execute the following commands:
It will open the browser to authenticate your user in the Azure portal.
->az acr login --name mycontainerstest
It will log in to your container registry created previously. Note, I am using my container instance named “mycontainerstest”.
->docker tag simpleapi mycontainerstest.azurecr.io/sample/simpleapi
This will tag our local image with the full name of our container registry.
->docker push mycontainerstest.azurecr.io/sample/simpleapi
It will now push our image to the container registry.
If we go to the Azure portal and open the Azure Container Registry, we can see in the repositories our image.
Running the image in the Azure Container Instance
Now we have everything set up to run our API in the cloud container, we will need to go to create a new resource and look for “Azure Container Instances”.
In the image source, select our container registry “mycontainerstest” and our image.
After creating the container instance, navigate to it and look for the public IP Address in the properties. Then navigate to it adding the sample endpoint to see the results. In the image below you can see the IP.
The image below is the container with the API running on the Azure cloud.
Hosting Docker Containers in Azure Container Instances References
If you want to know more about Azure Container Instances there is Microsoft documentation https://docs.microsoft.com/en-us/azure/container-instances/ where you can dive deeper. And if you want to learn more about Docker, you can find out more on the Docker website https://docs.docker.com/.
There are many ways to run an application that resides in a docker container. For example, you can start a virtual machine (VM) and use docker commands to run the container inside it, or you could deploy to Azure Kubernetes Services or Azure Container Apps. It is often difficult to work out which one to use.
Suppose you are looking to deploy a simple application based on a single container, or a small number of containers with little or no orchestration needed to manage them. In that case, Azure Container Instances are a simple and effective way to deploy your application.
If you would like outsourced help with your Docker or Azure custom development, get in touch and speak to one of our consultants.