📦Working with container images and Azure Container Registry

Leverage Infrastructure-As-Code for Azure to deploy an instance of Azure Container Registry and work with container images.

Working with container images and Azure Container Registry

This article will provide guidance on using Azure Bicep to deploy an instance of Azure Container Registry and work with container images.

We will perform the following steps:

  1. Create an Azure Container Registry by using a Bicep File.
  2. Deploy a container image to Container Registry
  3. Deploy an Azure container instance

Table of Contents

· Prerequisites
· Solution Overview
· 1. Create an Azure Container Registry by using a Bicep File.
· 2. Deploy a container image to Container Registry
Validate your container image in Container Registry
· 3. Deploy an Azure container instance
· Source Code

Prerequisites

Solution Overview

The solution will include the following files:

  • 📄 main.bicep: This is the Bicep template
  • 📄 azuredeploy.parameters.json: This parameter file contains the values to use for deploying your Bicep template.

Let’s get started!

1. Create an Azure Container Registry by using a Bicep File.

Create a new file named main.bicep . The code below will create the Azure Container Registry:

@description('Provide a globally unique name of your Azure Container Registry')
param acrName string = 'azinsideracr${uniqueString(resourceGroup().id)}'
@description('Provide a location for the registry.')
param location string = resourceGroup().location
@description('Provide a tier of your Azure Container Registry.')
param acrSku string = 'Basic'
//This creates the Azure Container Registry
resource acrResource 'Microsoft.ContainerRegistry/registries@2021-06-01-preview' = {
name: acrName
location: location
sku: {
name: acrSku
}
properties: {
adminUserEnabled: false
}
}
@description('Output the login server property for later use')
output loginServer string = acrResource.properties.loginServer

Then, we will create a new file named azuredeploy.parameters.json to define the actual value of the parameters. Below is an example with a single parameter for the location:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"value": "eastus"
}
}
}

We will use the following script to deploy the above Azure Bicep file:

Script to deploy Azure Bicep file

The image below shows the preview of the deployment:

Deployment preview — Azure container registry

Then, we will execute the deployment. The image below shows the deployment output:

Azure Bicep deployment output — Azure container registry

Note we defined an output value named loginServer to get the Login Server value from the container registry.

Now we will deploy a container image to our Container Registry.

2. Deploy a container image to Container Registry

On the Azure portal’s navigation pane, select the Cloud Shell icon to open a new shell instance.

Once the Cloud Shell is ready, run the following command to move from the root directory to the ~/clouddrive directory:

cd ~/clouddrive

Run the following command to create a new directory named azinsider in the ~/clouddrive directory:

mkdir azinsider

Run the following command to change the active directory from ~/clouddrive to ~/clouddrive/azinsider:

cd ~/clouddrive/azinsider
Cloud Shell

Now we will use Azure Container Registry to build and push an image.

First, we will create a working directory and create a Dockerfile named Dockerfile with the single line:

echo "FROM mcr.microsoft.com/hello-world" > Dockerfile

This is a simple example to build a Linux container image from the hello-world image hosted at Microsoft Container Registry.

Next, we will use the command below to build the image and, after the image is successfully built, push it to the container registry.

az acr build --image sample/hello-world:v1 \
--registry azinsideracrombqymkr3dzkg \
--file Dockerfile .

The above example builds and pushes the sample/hello-world:v1 image. The . at the end of the command sets the location of the Dockerfile, in this case, the current directory.

**Remember to update the above command with the name of your container registry.

Output from a successful build and push is similar to the following:

Now let’s run the image!

az acr run --registry azinsideracrombqymkr3dzkg \
--cmd '$Registry/sample/hello-world:v1' /dev/null

You should see an output similar to the below:

Validate your container image in Container Registry

In the Azure Portal, from your container registry, go to the repositories section and you should see the container image as shown below:

Azure Container Registry -container image

Now let’s deploy an Azure container instance.

3. Deploy an Azure container instance

First, we need to enable the admin user in Container Registry. You can go to the Container Registry, select Access Keys and then enable the admin user:

Now, you can go back to the Repositories section, on the Repository blade, select the ellipsis menu associated with the latest tag entry, and then select Run instance.

Then, you can provide the details to run the container image:

You can review the status of the container in the Azure Container Instance blade and then go to logs:

Source Code

👉 Join the AzInsider email list here.

-Dave R.

--

--