Edit

Share via


Tutorial - Create an Azure Container Registry (ACR) and build images

Azure Container Registry (ACR) is a private registry for container images. A private container registry allows you to securely build and deploy your applications and custom code.

In this tutorial, you deploy an ACR instance and push a container image to it. You learn how to:

  • Create an ACR instance.
  • Use ACR Tasks to build and push container images to ACR.
  • View images in your registry.

Before you begin

In the previous tutorial, you cloned the application code repository and used Docker to create a container image for a simple Azure Store Front application. If you didn't create the Azure Store Front app image, return to Tutorial 1 - Prepare an application for AKS.

This tutorial requires Azure CLI version 2.0.53 or later. To find the version, run the az --version command. If you need to install or upgrade, see Install Azure CLI.

Create an Azure Container Registry

Before creating an ACR instance, you need a resource group. An Azure resource group is a logical container into which you deploy and manage Azure resources.

  1. Create variables for the resource group name, location, and registry name. You can use these values or create your own. The registry name variable's value stored in ACRNAME must be unique within Azure and contain 5-50 lowercase alphanumeric characters.

    export RESOURCE_GROUP=myResourceGroup
    export LOCATION=westus2
    export RANDOM_STRING=$(printf '%05d%05d' "$RANDOM" "$RANDOM")
    export ACRNAME="myregistry${RANDOM_STRING}"
    

    The registry name variable's value stored in ACRNAME must be unique within Azure and contain 5-50 lowercase alphanumeric characters. The ACRNAME value is concatenated with the RANDOM_STRING variable that stores a random 10-digit string to ensure the registry name is unique. The variable RESOURCE_GROUP with the value myResourceGroup for the resource group and LOCATION with the value westus2. You can use these values or create your own.

  2. Create a resource group using the az group create command.

    az group create --name $RESOURCE_GROUP --location $LOCATION
    
  3. Create an ACR instance using the az acr create command and provide your own unique registry name. The Basic SKU is a cost-optimized entry point for development purposes that provides a balance of storage and throughput.

    az acr create \
      --resource-group $RESOURCE_GROUP \
      --location $LOCATION \
      --name $ACRNAME \
      --sku Basic
    

Build and push container images to registry

Build and push the images to your ACR using the Azure CLI az acr build command. The az acr build commands use images in the repository you cloned in the previous article prepare an app for AKS. Make sure you switch to that directory or the build commands fail. For example, if you created directory demorepo and cloned the repository, the repository's root directory is aks-store-demo, so switch to the demorepo/aks-store-demo directory.

There isn't an equivalent Azure PowerShell cmdlet that builds or pushes container images to the registry. You need to use the steps for Azure CLI, but with the ACRNAME variable set to the value you created in PowerShell. In PowerShell you can get the value with the command $ACRNAME.

In the following example, we don't build the product-service image. This image can take a long time to build, and there's a container image already available in the GitHub Container Registry (GHCR). You can use the az acr import command to import the image from the GHCR to your ACR instance. We also don't build the rabbitmq image. This image is available from the Docker Hub public repository and doesn't need to be built or pushed to your ACR instance.

az acr import \
  --name $ACRNAME \
  --source ghcr.io/azure-samples/aks-store-demo/product-service:latest \
  --image aks-store-demo/product-service:latest

az acr build \
  --registry $ACRNAME \
  --image aks-store-demo/order-service:latest ./src/order-service/

az acr build \
  --registry $ACRNAME \
  --image aks-store-demo/store-front:latest ./src/store-front/

List images in registry

View the images in your ACR instance using the az acr repository list command.

az acr repository list --name $ACRNAME --output table

The following example output lists the available images in your registry:

Result
----------------
aks-store-demo/product-service
aks-store-demo/order-service
aks-store-demo/store-front

Next steps

In this tutorial, you created an ACR and pushed images to it to use in an AKS cluster. You learned how to:

  • Create an ACR instance.
  • Use ACR Tasks to build and push container images to ACR.
  • View images in your registry.

In the next tutorial, you learn how to deploy a Kubernetes cluster in Azure.