Background


Sometimes, there are instances where we need to create a Docker image but do not have access to a Docker environment or prefer not to set up a virtual machine solely for installing Docker. Of course, we can do this with GitHub Actions, but there is an alternative option that I would like to share in this post: utilizing Azure Container Registry to build your Docker image.

Steps


Create Azure Container Registry

Azure Container Registry is a managed registry service based on the open-source Docker Registry 2.0. Create and maintain Azure container registries to store and manage your container images and related artifacts.

Use Azure container registries with your existing container development and deployment pipelines, or use Azure Container Registry Tasks to build container images in Azure. Build on demand, or fully automate builds with triggers such as source code commits and base image updates.

You can find and create an Azure Container Registry service from Azure Portal. I've already created one.

Use ACR Tasks

ACR Tasks is a suite of features within Azure Container Registry. It provides cloud-based container image building for platforms including Linux, Windows, and ARM, and can automate OS and framework patching for your Docker containers.

To utilize ACR Tasks, you need to access the Azure CLI command line. You have two options for doing so:

  1. Install Azure CLI locally on your machine.
  2. Use Azure Cloud Shell, which allows you to access the Azure CLI command line directly within your browser.

This will open a command line blade under your screen. You can choose PowerShell or Bash as you wish.

Get source code

The sample project in this post is Elf (https://github.com/EdiWang/Elf), it is the link forward service used by https://go.edi.wang. It generates static URLs for redirecting third party URLs. 

First, clone it into Azure Cloud Shell's home directory.

git clone https://github.com/EdiWang/Elf.git

As you can see, this repo has a Dockerfile, which we will use to build the Docker image.

Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.

Build Docker Image

Firstly, if you have multiple Azure subscriptions in your account, you need to set the current context to use the subscription in which the Azure Container Registry is created.

az account set --subscription "DevTest"

Then, run the az acr build command to build the docker image. In my case, the command will be

az acr build -t ediwang/elf:acrdemo -r ediwang .

The -r ediwang refers to the Azure Container Registry name.

Now, ACR Tasks will begin to build your docker image.

After it is completed, you can find your image in Repositories blade of the Azure Container Registry.

Conclusion


In this post, we used ACR Tasks from Azure CLI to build Docker image without setting up a Docker environment either on local machine or a cloud VM.