Master Azure Docker Deployment: Step-by-Step Guide

Published:2 October 2019 - 7 min. read

Peter De Tender Image

Peter De Tender

Read more tutorials by Peter De Tender!

For this ATA Project, you’re going to get a glimpse into how Docker in Azure works. In this Azure Docker tutorial, you’re going to learn about the Azure Container Instance (ACI) service by Microsoft.

Not a reader? Watch this related video tutorial!
Not seeing the video? Make sure your ad blocker is disabled.

Azure Container Instance is a hosted Kubernetes cluster via Azure Docker hosting. It allows you to deploy any Docker image (Windows and Linux) and run it as a container. Each Azure Docker container runs as a stand-alone container, using its own allocated CPU and memory.

In this project, you’ll learn how to deploy and run your first Azure Container Instance, based on a sample ASP.NET container image from Microsoft in the Azure public cloud.

By the end of this project, you’ll have an ASP.NET website running in a Docker container hosted by the Azure Container Instance service.

Environment and Knowledge Requirements

To get the most out of this Azure Docker tutorial, you should have the following:

  1. A local Windows 10 client with Docker Desktop installed.
  2. A Microsoft Azure account
  3. Access to an Azure account with permissions to deploy a container to Azure Container Registry
  4. The Windows Azure CLI installed. You’ll be learning from the command line.
  5. A beginner understanding of containers and Docker.
  6. A registered Docker ID.

If you don’t like the example chosen in this Azure Docker tutorial, you can always use another container image on the Docker Hub.

Without further ado, let’s get into building this Project!

Running a Docker Container

To avoid frustration later on when something is not working, always first validate the Docker Engine is running. To do so, open a command prompt or PowerShell session and run docker info.

You can see below the type of output you should receive.

Inspecting information about the local Docker installation
Inspecting information about the local Docker installation

To validate bring up a Docker container, run docker run hello-world. Behind the scenes the docker command performs a few steps:

  1. Looks for a container called hello-word locally on your Windows 10 computer.
  2. When not found, it then reaches out to the public Docker Hub repository to look for the latest version of a container with the name hello-world.
  3. When the container is found, it’s then downloaded from the Docker Hub repository.
  4. Once downloaded, the container is brought up and executes startup instructions provided within the image the container is using.

You can see some example output below.

Download a sample Docker container called hello-world
Download a sample Docker container called hello-world

Starting a Sample ASP.NET Container

Now that you know how to download a Docker container and run it, now download and run the container you’ll be working within this Project. This Docker container is an ASP.Net Core web app image.

To download this Azure Docker tutorial’s image, run the following command:

> docker run --name aspnetcore_sample --rm -it -p 8000:80 mcr.microsoft.com/dotnet/core/samples:aspnetapp

This command does the following:

  1. Since the Docker image isn’t available locally, Docker downloads a container called aspnetapp from another public container registry hosted by Microsoft located at mcr.microsoft.com in the dotnet/core/samples directory. The mcr.microsoft.com container registry is similar to the public Docker Hub but is maintained by Microsoft and only hosts Microsoft container images.
  2. Runs the container on your local computer.
  3. (-p 8000:80) – Maps the listening TCP port 80 on the container to TCP port 8000. This allows you to access the web app at port 8000 locally.
  4. (-it) – The container will be launch interactively, meaning you will be immediately brought into the console when it starts.
  5. (—rm) – When the container is stopped, it will be removed.

You can see below that output you can expect.

Downloading a container instance from Microsoft
Downloading a container instance from Microsoft

6.   From your browser, connect to this running container application by browsing to http://localhost:8000. You should see the ASP.Net sample web app

Sample ASP.NET web app
Sample ASP.NET web app

Creating an Azure Container Registry (ACR) Instance

Once you have the ASP.NET container downloaded to your local computer, it’s now time to migrate it to Azure Docker hosting with ACI and run this container in Azure. To do this, we’ll push the demo container to Azure Container Registry (ACR). ACR is a service that allows you to deploy and run Docker containers directly within Azure.

  1. Log into the Azure Portal.
  2. Click on the terminal icon at the top to start Azure Cloud Shell.
Starting Azure Cloud Shell
Starting Azure Cloud Shell

3.   Make sure you choose the Bash shell as shown below.

Bash in Azure Cloud Shell
Bash in Azure Cloud Shell

4.   Create an Azure resource group to put the an instance of ACR in using the az group create command below.

> az group create --name <RGname> --location <Azure Region of choice>
Creating a resource group
Creating a resource group

5.   Now create the ACR inside of the resource group using the az acr create command. Below you can see an example of creating an ACR with a Basic SKU using the admin-enabled parameter set to true. This is set to true because it allows you to perform resource management later on.

> az acr create --resource-group <RGname> --name <ACRname> --sku Basic
 --admin-enabled true
Creating an Azure Container Registry
Creating an Azure Container Registry

Once you’re done, you should now have an ACR instance running inside of your resource group. Azure Docker hosting to the rescue!

Connecting to the ACR Instance Locally

Now you need to move from the Azure Cloud Shell to your local Windows computer to finish with the set up.

  1. On your local machine, launch PowerShell or a command prompt with administrative rights. I’ll be using PowerShell.
  2. Authenticate to Azure with the Azure CLI by running az login and follow the instructions to provide your credentials.
  3. Once authenticated successfully, you should see some JSON output indicating the user you just authenticated with.
Authenticating to Azure
Authenticating to Azure

4.   Next, authenticate to the ACR instance you just created in the Azure Cloud Shell using the as acr login command using the below command. Below you can see I’m connecting to an ACR instance called nopacr1, in Azure resource group NOPRG.

> az acr login --name <ACR-Name> --resource-group <RG Name>
Logging onto ACR
Logging onto ACR

Updating The Docker Image Tag

You’re now ready to push your local Docker image to your ACR instance in Azure Docker hosting or ACI. But you must first fulfill an ACR dependency. All Docker images pushed to ACR instance must have the name of the ACR instance in the image name.

  1. Find the Docker image ID number or image name using the command docker images.
Finding the ACR Docker image
Finding the ACR Docker image

2.   Update the Docker image tag for the Docker image by running the docker tag command as shown below. If you’re following the naming convention, use the ACR name of NOPACR1 and define the tag as nopacr1.azurecr.io. For the image itself, use the name dotnetsample.

> docker tag mcr.microsoft.com/dotnet/core/samples:aspnetapp <ACRName>.azurecr.io/<imagenameofchoice>
Updating the Docker tag
Updating the Docker tag

3.   Verify the tag has been applied using the docker images command again. You should now see the tag has been updated.

Verifying Docker tag
Verifying Docker tag

Uploading the Docker Image to the ACR Instance and Verify

  1. Now upload the local image to the ACR instance using the docker push command as shown below providing the dns name label where the ACR instance is located.
> docker push <ACRName>.azurecr.io/<nameofimage>
Pushing Docker image to ACR
Pushing Docker image to ACR

2.   Once uploaded, verify the push to the ACR instance went fine by using the Azure Portal by navigating to All Services —> Azure Container Registries and selecting the ACR instance you created earlier.

Inspecting ACR instance in Azure portal
Inspecting ACR instance in Azure portal

3.   In the blade menu to the left under the Services section, click Repositories. The Docker image just pushed the your ACR instance, should show up here.

Inspecting Docker repo
Inspecting Docker repo

4.   Click on the image and you will see it’s version and some other useful information.

Inspecting Docker image
Inspecting Docker image

Running the Azure Container Instance (ACI)

You’ve now created an ACR instance, tagged a Docker container image with the ACR name as a reference, and pushed the image to the ACR instance. Now it’s time to use Azure Container Instance (ACI) to run the Docker image stored in your ACR instance.

  1. Continuing from the previous step, click the … next to latest, and choose Run instance. This will open the Create Container Instance blade.
Creating a container instance
Creating a container instance

2.   Complete the fields in the Create container instance box below using the following information:

  • Container Name – any name you’d like to use for this running container
  • OS type – Linux (This depends on the source container. If you’re using the same container in this project, this may be Windows.)
  • Subscription – your Azure Subscription
  • Resource Group – any existing or new Azure Resource Group to add the container instance into
  • Location – based on the resource group

For this Azure Docker tutorial ASP.NET container, you can leave all other settings unchanged. In a production environment, depending what the container is running, you might need to change these settings.

Creating a container instance
Creating a container instance

3.   Press OK to create the Container instance.
4.   Once the container instance has been deployed, you should see a page that looks like the example below.

Successful container instance deployment
Successful container instance deployment

5.   Once the deployment is finished, open the ACI in the portal by navigating to All Services —> Container Instances. Once here, browse to the Azure Container Instance (ACI) that just got created. You should see the instance shown below.

Inspecting created container instance
Inspecting created container instance

6.   Copy the IP address in the upper-right corner and paste it into a web browser. Your sample ASP.NET web app should load from your Azure Container Instance (ACI) and display a web page as shown below.

Sample ASP.NET Core webpage
Sample ASP.NET Core webpage

If you’re done with this container, you can then use the az container delete command to remove the container.

Conclusion

This Azure Docker tutorial should have given you a foundational understanding of ACI and how the Azure stores Docker images.

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!