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.
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.
Table of Contents
Environment and Knowledge Requirements
To get the most out of this Azure Docker tutorial, you should have the following:
- A local Windows 10 client with Docker Desktop installed.
- A Microsoft Azure account
- Access to an Azure account with permissions to deploy a container to Azure Container Registry
- The Windows Azure CLI installed. You’ll be learning from the command line.
- A beginner understanding of containers and Docker.
- 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
You can see below the type of output you should receive.
To validate bring up a Docker container, run
docker run hello-world. Behind the scenes the
docker command performs a few steps:
- Looks for a container called hello-word locally on your Windows 10 computer.
- 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.
- When the container is found, it’s then downloaded from the Docker Hub repository.
- 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.
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:
- 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.
- Runs the container on your local computer.
- (-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.
- (-it) – The container will be launch interactively, meaning you will be immediately brought into the console when it starts.
—rm) – When the container is stopped, it will be removed.
You can see below that output you can expect.
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
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.
3. Make sure you choose the Bash shell as shown below.
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>
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
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.
- On your local machine, launch PowerShell or a command prompt with administrative rights. I’ll be using PowerShell.
- Authenticate to Azure with the Azure CLI by running
az loginand follow the instructions to provide your credentials.
- Once authenticated successfully, you should see some JSON output indicating the user you just authenticated with.
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>
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.
- Find the Docker image ID number or image name using the command
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>
3. Verify the tag has been applied using the
docker images command again. You should now see the tag has been updated.
Uploading the Docker Image to the ACR Instance and Verify
- Now upload the local image to the ACR instance using the
docker pushcommand as shown below providing the dns name label where the ACR instance is located.
> docker push <ACRName>.azurecr.io/<nameofimage>
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.
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.
4. Click on the image and you will see it’s version and some other useful information.
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.
- Continuing from the previous step, click the … next to latest, and choose Run instance. This will open the Create Container Instance blade.
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.
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.
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.
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.
If you’re done with this container, you can then use the
az container delete command to remove the container.
This Azure Docker tutorial should have given you a foundational understanding of ACI and how the Azure stores Docker images.
More from Adam The Automator & Friends