Running Containers from Azure Container Registry (ACR)

Running Containers from Azure Container Registry (ACR)

In this demo-focused blog post, I'll create an Azure Container Registry (ACR), store a Docker image in ACR, and run an instance of the image in Azure Container Instances. I'll mainly stick with the Azure CLI and run commands in the Azure Cloud Shell.

  • To create an Azure Container Registry, follow these steps in the command line window (assuming you are working in a shell window with Azure CLI enabled):

    1. Create the necessary variables and assign their values.

    2. Use the Azure CLI command listed below to create your Azure Container Registry, and ensure to set the 'admin-enable' property to true, as it will be required in the subsequent steps.

    3. Go to the Resource Group to confirm that the Azure Container Registry has been successfully created.

$rgname = "your resource group name" ` 
$random = Get-Random `
$acrname = "acr$random" `
az acr create --resource-group $rgname --name $acrname --sku Basic `
az acr list ## Verify creation of the ACR
az acr update -n $acrname --admin-enabled true `

  • To add container images to the Azure Container Registry (ACR), follow these steps:

    1. Create a new folder.

    2. Navigate into the newly created folder.

    3. Create a Dockerfile that pulls a simple 'hello-world' image from the Microsoft Artifact Registry, which is a public hub for container images.

echo FROM mcr.microsoft.com/hello-world > Dockerfile
  • It is well-known that syntax errors, such as code breaking into new lines, can lead to issues during the execution stage. To check your Dockerfile for syntax errors, open the Dockerfile you've just created and ensure that the 'FROM' command and the subsequent statements are properly aligned on a single line.

  • You're now ready to run your Dockerfile, which pulls a container image from the public container image hub and stores it in your Azure Container Registry (ACR). Use the following code to create your first container image in your ACR.
az acr build --image sample/hello-world:v1 --registry $acrname --file Dockerfile .
  • To verify the creation of the container image, go to your ACR resource in the portal. On the left menu, click on the 'Repositories' section, and check if your image is listed properly under the 'Repositories' list.

  • It's time to create an Azure Container Instance using the container image hosted in ACR. Running the code below will create an Azure Container Instance with defined properties, including the name, resource group, and image registry information with its version appended. You can retrieve the image details by listing them using the 'az acr list' command.
az container create --resource-group $rgname --name "ci$random" --image acr1243964625.azurecr.io/sample/hello-world:v1
  • To verify if the container instance creation was successful, follow these steps:

    1. Use the top search box to search for 'Container Instances' and navigate to the 'Container Instances' page in the Azure portal.

    2. In the command line, execute the 'az container list' CLI command to view the list of containers and confirm that the newly created container is listed.

  • In the Azure portal, navigate to the 'Container Instances' page. On the left menu, select 'Containers,' then go to the 'Logs' tab. Check if the 'Hello World' message appears in the log. This confirms that your installation is successful and the container image has been ran correctly.