Edit

Share via


Use a managed identity to authenticate to an Azure container registry

You can use a managed identity for Azure resources to authenticate to an Azure container registry from another Azure resource, without needing to provide or manage registry credentials.

For example, set up a user-assigned or system-assigned managed identity on a Linux VM to access container images from your container registry, as easily as you use a public registry. Or, set up an Azure Kubernetes Service cluster to use its managed identity to pull container images from Azure Container Registry for pod deployments.

For an overview of managed identities in Azure, see What is managed identities for Azure resources?

You can assign a managed identity a role with pull only, push and pull, or other permissions to one or more private registries in Azure. For a complete list of registry roles, see Azure Container Registry permissions and roles overview.

Then, use the identity to authenticate to any service that supports managed identities, without requiring any credentials in your code.

In this article, you learn how to:

  • Enable a user-assigned or system-assigned identity on an Azure virtual machine (VM)
  • Grant the identity access to an Azure container registry
  • Use the managed identity to access the registry and pull a container image

Prerequisites

You can use either Azure CLI or Azure PowerShell to complete the steps in this article. Use the most recent version of either tool. If you don't have either tool installed, see Install the Azure CLI or Install Azure PowerShell.

To set up a container registry and push a container image to it, you must also have Docker installed locally. Docker provides packages that easily configure Docker on any macOS, Windows, or Linux system.

If you don't already have an Azure container registry, create a registry and push a sample container image to it. Follow the steps to create a registry by using the Azure CLI, Azure PowerShell, or the Azure portal.

This article assumes you have the aci-helloworld:v1 container image stored in your registry. The examples use a registry name of myContainerRegistry. You can replace these values with your own registry and image names.

Create and configure a Docker-enabled VM

Create a Docker-enabled Ubuntu VM to use with your managed identity. You also need to install either the Azure CLI or Azure PowerShell on the virtual machine, depending on which tool you want to use.

If you already have an Azure VM with Azure CLI or Azure PowerShell installed, you can move to the next section.

Create the VM

Deploy a default Ubuntu Azure virtual machine by using az vm create. The following example creates a VM named myDockerVM in an existing resource group named myResourceGroup:

az vm create \
    --resource-group myResourceGroup \
    --name myDockerVM \
    --image Ubuntu2204 \
    --admin-username azureuser \
    --generate-ssh-keys

It takes a few minutes to create the VM. When the command finishes, note the publicIpAddress displayed by the Azure CLI. Use this address to make SSH connections to the VM in the next step.

Install Docker on the VM

Next, install Docker on your VM so that it can pull and run container images from your Azure Container Registry.

Once the VM is running, make an SSH connection to the VM. Replace publicIpAddress with the public IP address of your VM.

ssh azureuser@publicIpAddress

Run the following command to install Docker on the VM:

sudo apt update
sudo apt install docker.io -y

After installation, run the following command to verify that Docker is running properly on the VM:

sudo docker run -it mcr.microsoft.com/hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
[...]

Install Azure CLI or Azure PowerShell on the VM

Follow the steps in Install Azure CLI with apt to install the Azure CLI on your Ubuntu virtual machine. For this article, be sure to install the most recent version.

After installing the Azure CLI, exit the SSH session.

Configure the VM with a user-assigned managed identity

A user-assigned managed identity is a standalone Azure resource that you manage separately from the resources that use it. You can associate a user-assigned managed identity with multiple Azure resources.

This section explains how to configure your VM with a user-assigned identity to securely access your Azure Container Registry.

Create a user-assigned managed identity

Create an identity in your subscription by using the az identity create command. You can use the same resource group you used previously to create the container registry or virtual machine, or choose a different one.

az identity create --resource-group myResourceGroup --name myACRId

To configure the identity in the following steps, use the az identity show command to store the identity's resource ID and service principal ID in variables.

# Get resource ID of the user-assigned identity
userID=$(az identity show --resource-group myResourceGroup --name myACRId --query id --output tsv)

# Get service principal ID of the user-assigned identity
spID=$(az identity show --resource-group myResourceGroup --name myACRId --query principalId --output tsv)

Because you need the identity's ID in a later step when you sign in to the CLI from your virtual machine, show the value:

echo $userID

The ID is of the form:

/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId

Configure the VM with the user-assigned managed identity

Next, configure your Docker VM to use the user-assigned identity you created in the previous step.

Use az vm identity assign to configure the VM, using the ID you retrieved in the previous step:

az vm identity assign --resource-group myResourceGroup --name myDockerVM --identities $userID

Grant the user-assigned managed identity access to the container registry

Now configure the identity so that it has access to your container registry.

You must assign either Container Registry Repository Reader (for ABAC-enabled registries) or AcrPull (for non-ABAC registries). This role assignment provides pull permissions to the registry.

To provide both pull and push permissions, assign either the Container Registry Repository Writer role for ABAC-enabled registries, or the AcrPush role for non-ABAC-enabled registries.

Use az acr show to get the resource ID of the registry:

resourceID=$(az acr show --resource-group myResourceGroup --name myContainerRegistry --query id --output tsv)

Use az role assignment create to assign the desired role to the identity. This example assigns the Container Registry Repository Reader role, which grants pull permissions only, on an ABAC-enabled registry:

az role assignment create --assignee $spID --scope $resourceID \
    --role "Container Registry Repository Reader" # For ABAC-enabled registries. Otherwise, use AcrPull for non-ABAC registries.

Use the user-assigned managed identity to access the registry

SSH into the Docker virtual machine that the identity configures, and then run the following commands to enable the identity to access your container registry.

On the VM, authenticate to the Azure CLI by using az login, using the identity you retrieved earlier.

az login --identity --username <userID>

Next, authenticate to the registry by using az acr login. When you use this command, the CLI uses the Active Directory token created when you ran az login to seamlessly authenticate your session with the container registry.

az acr login --name myContainerRegistry

You should see a Login succeeded message. You can then run docker commands without providing credentials. For example, run docker pull to pull the aci-helloworld:v1 image, specifying the login server name of your registry:

docker pull mycontainerregistry.azurecr.io/aci-helloworld:v1

Configure the VM with a system-assigned managed identity

A system-assigned managed identity is a feature of Azure that allows your virtual machine to automatically manage its own identity in Azure Active Directory. This section explains how to configure your VM with a system-assigned identity to securely access your Azure Container Registry.

Enable the system-assigned managed identity on your VM

Use az vm identity assign to configure your Docker VM with a system-assigned identity:

az vm identity assign --resource-group myResourceGroup --name myDockerVM

Use az vm show to set a variable to the value of principalId (the service principal ID) of the VM's identity, to use in later steps:

spID=$(az vm show --resource-group myResourceGroup --name myDockerVM --query identity.principalId --out tsv)

Grant the system-assigned managed identity access to the container registry

Now configure the identity so that it has access to your container registry.

You must assign either Container Registry Repository Reader (for ABAC-enabled registries) or AcrPull (for non-ABAC registries). This role assignment provides pull permissions to the registry.

To provide both pull and push permissions, assign either the Container Registry Repository Writer role for ABAC-enabled registries, or the AcrPush role for non-ABAC-enabled registries.

Use az acr show to get the resource ID of the registry:

resourceID=$(az acr show --resource-group myResourceGroup --name myContainerRegistry --query id --output tsv)

Use az role assignment create to assign the desired role to the identity. This example assigns the Container Registry Repository Reader role, which grants pull permissions only, on an ABAC-enabled registry:

az role assignment create --assignee $spID --scope $resourceID \
    --role "Container Registry Repository Reader"

Use the system-assigned managed identity to access the registry

SSH into the Docker virtual machine that the identity configures, and then run the following commands to enable the identity to access your container registry.

On the VM, authenticate the Azure CLI by using az login, using the system-assigned identity.

az login --identity

Next, authenticate to the registry by using az acr login. When you use this command, the CLI uses the Active Directory token created when you ran az login to seamlessly authenticate your session with the container registry.

az acr login --name myContainerRegistry

You should see a Login succeeded message. You can then run docker commands without providing credentials. For example, run docker pull to pull the aci-helloworld:v1 image, specifying the login server name of your registry:

docker pull mycontainerregistry.azurecr.io/aci-helloworld:v1