Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this article, you will learn how to enable the artifact cache feature in your Azure Container Registry (ACR) to cache images from another Azure Container Registry. The downstream registry authenticates to the upstream registry using a managed identity, so you don't need to store credentials in Azure Key Vault.
In addition to the prerequisites listed here, you need an Azure account with an active subscription. Create a trial subscription.
Prerequisites
- An existing downstream ACR instance (referred to as
MyRegistryin this article). If you don't already have one, create a new container registry. - An existing upstream ACR instance (referred to as
UpstreamRegistryin this article) that contains the artifacts you want to cache. - Azure CLI version 2.85.0 or later. You can use the Azure CLI local installation. To confirm your version, run
az --version. To install or upgrade, see Install Azure CLI.
- Bicep tools (for Bicep deployments).
Configure artifact cache
Create a user-assigned managed identity
ACR-to-ACR caching authenticates to the upstream registry using a user-assigned managed identity instead of username and password credentials stored in Key Vault. You need to create a managed identity and grant it pull access to the upstream registry.
Create a user-assigned managed identity:
az identity create \ --name MyACRCacheIdentity \ --resource-group MyResourceGroupGet the principal ID and resource ID of the managed identity:
IDENTITY_PRINCIPAL_ID=$(az identity show \ --name MyACRCacheIdentity \ --resource-group MyResourceGroup \ --query 'principalId' \ -o tsv) IDENTITY_RESOURCE_ID=$(az identity show \ --name MyACRCacheIdentity \ --resource-group MyResourceGroup \ --query 'id' \ -o tsv)
Assign pull permissions on the upstream registry
The upstream registry must have ABAC (Attribute-Based Access Control) enabled so you can assign fine-grained repository permissions. The managed identity needs the Container Registry Repository Reader role on the upstream registry, scoped to the specific repository you want to cache.
Note
If the upstream registry doesn't have ABAC enabled yet, run az acr update --name UpstreamRegistry --role-assignment-mode rbac-abac.
Get the resource ID of the upstream registry:
UPSTREAM_ID=$(az acr show \ --name UpstreamRegistry \ --query 'id' \ -o tsv)Assign the Container Registry Repository Reader role to the managed identity on the upstream registry:
az role assignment create \ --role "Container Registry Repository Reader" \ --assignee "$IDENTITY_PRINCIPAL_ID" \ --scope "$UPSTREAM_ID/repositories/myapp"
Create a cache rule
Create a cache rule that pulls artifacts from the upstream registry into your downstream registry.
Run
az acr cache createto create a cache rule. Use the--identityparameter to specify the user-assigned managed identity for authentication with the upstream registry:az acr cache create \ -r MyRegistry \ -n MyRule \ -s upstreamregistry.azurecr.cn/myapp \ -t myapp \ --identity "$IDENTITY_RESOURCE_ID"Run
az acr cache showto verify the cache rule:az acr cache show -r MyRegistry -n MyRule
Verify the cache
After you configure the cache rule and assign the required permissions, pull an image from your downstream registry to verify that caching works correctly.
docker pull myregistry.azurecr.cn/myapp:latest
The first pull retrieves the image from the upstream registry and caches it in your downstream registry. Subsequent pulls are served directly from the downstream registry cache.
Clean up resources
When the cache resources are no longer needed, delete the cache rule, role assignment, and managed identity.
Delete the cache rule by running
az acr cache delete:az acr cache delete -r MyRegistry -n <cache-rule-name>Replace
<cache-rule-name>with the name you used when creating the cache rule (for example,MyRulefor the Azure CLI tab orcacheRulefor the Bicep tab).Remove the role assignment on the upstream registry:
UPSTREAM_ID=$(az acr show --name UpstreamRegistry --query 'id' -o tsv) IDENTITY_PRINCIPAL_ID=$(az identity show \ --name MyACRCacheIdentity \ --resource-group MyResourceGroup \ --query 'principalId' \ -o tsv) az role assignment delete \ --role "Container Registry Repository Reader" \ --assignee "$IDENTITY_PRINCIPAL_ID" \ --scope "$UPSTREAM_ID/repositories/myapp"Delete the user-assigned managed identity:
az identity delete \ --name MyACRCacheIdentity \ --resource-group MyResourceGroupNote
Make sure the identity is not in use by any other resources before deleting it.
Next steps
- Learn about troubleshooting issues with artifact caching.
- Learn about artifact cache overview.
- Learn how to enable artifact cache using the Azure portal.