Posted in:

If you're running containers in Azure, then Azure Container Registry (ACR) is a great place to store your custom container images.

But ACR can do more than store container images. It can also build them for you. This is really useful if like me, you sometimes have to work on a computer that doesn't have Docker installed.

In this post, I'll run you through the steps required to build container images using ACR, without needing Docker installed locally. And we'll also see how we can easily run the container with Azure Container Instances.

Prerequisites

First of all, you will need access to the Azure CLI. This can either be installed locally, or you can use the excellent Azure Cloud Shell which you can access in the Azure Portal, and means you don't need anything installed locally other than web browser.

Second, you will of course need an Azure Container Registry. You can create one in the Azure portal, or you can enter the following Azure CLI commands to create a new resource group and an ACR instance.

$resourceGroup = "AcrDemo"
az group create -n $resourceGroup -l westeurope
$acrName = "acrname"
az acr create -g $resourceGroup -n $acrName --sku Basic --admin-enabled

The Basic pricing tier is fine, and you do need the --admin-enabled flag if you want to be able to retrieve login credentials, which we'll use later.

Building an image with ACR

To build an image, you will of course need a Dockerfile, and all the supporting code files it needs. I recommend setting up a multi-stage build which allows a single Dockerfile to first build your application binaries and then use them to create the final container image. An example multi-stage Dockerfile for ASP.NET Core can be found here, and you should easily be able to find examples for other popular programming frameworks.

I've got a simple ASP.NET Core web app that I used in my recent Pluralsight course on deploying containers in Azure, and we'll use that as the demo scenario. If you'd like to follow the demo steps, clone the code with Git, and change into the SampleWebApp folder which contains a multi-stage build Dockerfile.

git clone https://github.com/markheath/azure-deploy-manage-containers.git
cd azure-deploy-manage-containers/SampleWebApp

Now, from within this folder, we can build using the az acr build command, specifying the ACR instance to use with -r, the Dockerfile to use with -f and the name and tag of the image to create with -t. The final argument (.), is simply the working folder.

az acr build -r $acrname -f .\multi-stage.Dockerfile -t samplewebapp:acr .

What this will do is zip up the Dockerfile and the local files in the working folder and upload them to your Azure Container Registry so it has all the context it needs to perform the build. This works whether you're running locally on a development machine or in the Azure Cloud Shell.

While it builds, you'll see output looking something like this:

Packing source code into tar to upload...
Excluding '.gitignore' based on default ignore rules
Uploading archived source code from 'C:\Users\mheath\AppData\Local\Temp\build_archive_397dfd61aaca4a2c95bb9e4ff791fee5.tar.gz'...
Sending context (172.888 KiB) to registry: pluralsightacr...
Queued a build with ID: cb4
Waiting for agent...
2018/11/19 14:40:25 Using acb_vol_7f5af0bc-f227-499f-94a4-0e2381be50dc as the home volume
2018/11/19 14:40:25 Setting up Docker configuration...
2018/11/19 14:40:25 Successfully set up Docker configuration
2018/11/19 14:40:25 Logging in to registry: pluralsightacr.azurecr.io
2018/11/19 14:40:32 Successfully logged in
2018/11/19 14:40:32 Executing step ID: build. Working directory: '', Network: ''
2018/11/19 14:40:32 Obtaining source code and scanning for dependencies...
2018/11/19 14:40:38 Successfully obtained source code and scanned for dependencies
Sending build context to Docker daemon  1.434MB
Step 1/10 : FROM microsoft/dotnet:2.1-sdk AS build-env
...
Run ID: cb4 was successful after 2m4s

And that's all there is to it - just a one line command to build our container image and store it in ACR.

Managing ACR images

If we want to explore the contents of our Azure Container Registry with the Azure CLI, we can use the following commands:

# list all images in our ACR
az acr repository list -n $acrName
# show the tags for the samplewebapp repository
az acr repository show-tags -n $acrName --repository samplewebapp
# show details for the samplewebapp:acr image
az acr repository show -n $acrName -t samplewebapp:acr

When we're done, if we want to delete this image, we can also use the Azure CLI to delete it:

# n.b. prompts, showing you exactly what will be deleted
az acr repository delete -n $acrName -t samplewebapp:acr

Run our container using ACI

Obviously, if we've not got Docker installed locally, we can't run our container image easily, but Azure has us covered on that front too. With Azure Container Instances, you can easily create a new container from that image with az container create. I'm using the -e flag to set an environment variable, and opening port 80 and giving it a custom DNS prefix.

Note that we will need a credentials for the ACR to do this, so I'm using az acr credential show to get hold of the ACR password.

# get the ACR password
$password = az acr credential show -n $acrName --query "passwords[0].value"  -o tsv

# create a new ACI instance to run this container
az container create -n samplewebapp -g $resourceGroup `
            --image "$acrName.azurecr.io/samplewebapp:acr" `
            --registry-username "$acrName" `
            --registry-password "$password" `
            -e TestSetting=ACI `
            --dns-name-label samplewebapp --ports 80

It will take a minute or so to get started, but once it's up and running, we can visit our website running in a container at samplewebapp.westeurope.azurecontainer.io.

Of course, don't forget to delete this container when you've done testing it. ACI is great for short-lived experiments, but its pricing model doesn't lend itself to leaving things running long-term. You can delete the container with:

az container delete -n samplewebapp -g $resourceGroup -y

Summary

Azure Container Registry is not only a great place to host container images but with the Azure CLI it's really easy to build images with ACR, eliminating the need to have a local installation of Docker. Azure Container Instances then makes it really easy to try out your container in an easily disposable serverless environment.

If you'd like to go deeper with these topics, I've created a few related Pluralsight courses you might find helpful: