|
| 1 | +--- |
| 2 | +title: Enable Caching for ACR with authentication - Azure CLI |
| 3 | +description: Learn how to enable Caching for ACR with authentication using Azure CLI. |
| 4 | +ms.topic: tutorial |
| 5 | +ms.date: 04/19/2022 |
| 6 | +ms.author: tejaswikolli |
| 7 | +--- |
| 8 | + |
| 9 | +# Enable Caching for ACR (Preview) with authentication - Azure CLI |
| 10 | + |
| 11 | +This article is part five of a six-part tutorial series. [Part one](tutorial-registry-cache.md) provides an overview of Caching for ACR, its features, benefits, and preview limitations. In [part two](tutorial-enable-registry-cache.md), you learn how to enable Caching for ACR feature by using the Azure portal. In [part three](tutorial-enable-registry-cache-cli.md), you learn how to enable Caching for ACR feature by using the Azure CLI. In [part four](tutorial-enable-registry-cache-auth.md), you learn how to enable Caching for ACR feature with authentication by using Azure portal. |
| 12 | + |
| 13 | +This article walks you through the steps of enabling Caching for ACR with authentication by using the Azure CLI. You have to use the Credential set to make an authenticated pull or to access a private repository. |
| 14 | + |
| 15 | +## Prerequisites |
| 16 | + |
| 17 | +* You can use the [Azure Cloud Shell][Azure Cloud Shell] or a local installation of the Azure CLI to run the command examples in this article. If you'd like to use it locally, version 2.0.74 or later is required. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][Install Azure CLI]. |
| 18 | +* You have an existing Key Vault to store credentials. Learn more about [creating and storing credentials in a Key Vault.][create-and-store-keyvault-credentials] |
| 19 | +* You can set and retrieve secrets from your Key Vault. Learn more about [set and retrieve a secret from Key Vault.][set-and-retrieve-a-secret] |
| 20 | + |
| 21 | +## Configure Caching for ACR (preview) with authentication - Azure CLI |
| 22 | + |
| 23 | +### Create a Credential Set - Azure CLI |
| 24 | + |
| 25 | +Before configuring a Credential Set, you have to create and store secrets in the Azure KeyVault and retrieve the secrets from the Key Vault. Learn more about [creating and storing credentials in a Key Vault.][create-and-store-keyvault-credentials] and to [set and retrieve a secret from Key Vault.][set-and-retrieve-a-secret]. |
| 26 | + |
| 27 | +1. Run [az acr credential set create][az-acr-credential-set-create] command to create a credential set. |
| 28 | + |
| 29 | + - For example, To create a credential set for a given `MyRegistry` Azure Container Registry. |
| 30 | + |
| 31 | + ```azurecli-interactive |
| 32 | + az acr credential-set create |
| 33 | + -r MyRegistry \ |
| 34 | + -n MyRule \ |
| 35 | + -l docker.io \ |
| 36 | + -u https://MyKeyvault.vault.azure.net/secrets/usernamesecret \ |
| 37 | + -p https://MyKeyvault.vault.azure.net/secrets/passwordsecret |
| 38 | + ``` |
| 39 | +
|
| 40 | +2. Run [az acr credential set update][az-acr-credential-set-update] to update the username or password KV secret ID on a credential set. |
| 41 | +
|
| 42 | + - For example, to update the username or password KV secret ID on a credential set a given `MyRegistry` Azure Container Registry. |
| 43 | +
|
| 44 | + ```azurecli-interactive |
| 45 | + az acr credential-set update -r MyRegistry -n MyRule -p https://MyKeyvault.vault.azure.net/secrets/newsecretname |
| 46 | + ``` |
| 47 | +
|
| 48 | +3. Run [az-acr-credential-set-show][az-acr-credential-set-show] to show a credential set. |
| 49 | +
|
| 50 | + - For example, to show a credential set for a given `MyRegistry` Azure Container Registry. |
| 51 | +
|
| 52 | + ```azurecli-interactive |
| 53 | + az acr credential-set show -r MyRegistry -n MyCredSet |
| 54 | + ``` |
| 55 | +
|
| 56 | +### Create a cache rule with a Credential Set - Azure CLI |
| 57 | +
|
| 58 | +1. Run [az acr cache create][az-acr-cache-create] command to create a cache rule. |
| 59 | +
|
| 60 | + - For example, to create a cache rule with a credential set for a given `MyRegistry` Azure Container Registry. |
| 61 | +
|
| 62 | + ```azurecli-interactive |
| 63 | + az acr cache create -r MyRegistry -n MyRule -s docker.io/library/ubuntu -t ubuntu -c MyCredSet |
| 64 | + ``` |
| 65 | +
|
| 66 | +2. Run [az acr cache update][az-acr-cache-update] command to update the credential set on a cache rule. |
| 67 | +
|
| 68 | + - For example, to update the credential set on a cache rule for a given `MyRegistry` Azure Container Registry. |
| 69 | +
|
| 70 | + ```azurecli-interactive |
| 71 | + az acr cache update -r MyRegistry -n MyRule -c NewCredSet |
| 72 | + ``` |
| 73 | +
|
| 74 | + - For example, to remove a credential set from an existing cache rule for a given `MyRegistry` Azure Container Registry. |
| 75 | +
|
| 76 | + ```azurecli-interactive |
| 77 | + az acr cache update -r MyRegistry -n MyRule --remove-cred-set |
| 78 | + ``` |
| 79 | +
|
| 80 | +3. Run [az acr cache show][az-acr-cache-show] command to show a cache rule. |
| 81 | +
|
| 82 | + - For example, to show a cache rule for a given `MyRegistry` Azure Container Registry. |
| 83 | + |
| 84 | + ```azurecli-interactive |
| 85 | + az acr cache show -r MyRegistry -n MyRule |
| 86 | + ``` |
| 87 | +
|
| 88 | +### Assign permissions to Key Vault |
| 89 | +
|
| 90 | +1. Get the principal ID of system identity in use to access Key Vault. |
| 91 | +
|
| 92 | + ```azurecli-interactive |
| 93 | + PRINCIPAL_ID=$(az acr credential-set show |
| 94 | + -n MyCredSet \ |
| 95 | + -r MyRegistry \ |
| 96 | + --query 'identity.principalId' \ |
| 97 | + -o tsv) |
| 98 | + ``` |
| 99 | +
|
| 100 | +2. Run the [az keyvault set-policy][az-keyvault-set-policy] command to assign access to the Key Vault, before pulling the image. |
| 101 | +
|
| 102 | + - For example, to assign permissions for the credential set access the KeyVault secret |
| 103 | +
|
| 104 | + ```azurecli-interactive |
| 105 | + az keyvault set-policy --name MyKeyVault \ |
| 106 | + --object-id $PRINCIPAL_ID \ |
| 107 | + --secret-permissions get |
| 108 | + ``` |
| 109 | +
|
| 110 | +### Pull your Image |
| 111 | +
|
| 112 | +1. Pull the image from your cache using the Docker command `docker pull myregistry.azurecr.io/hello-world` |
| 113 | +
|
| 114 | +## Clean up the resources |
| 115 | +
|
| 116 | +1. Run [az acr cache list][az-acr-cache-list] command to list the cache rules in the Azure Container Registry. |
| 117 | +
|
| 118 | + - For example, to list the cache rules for a given `MyRegistry` Azure Container Registry. |
| 119 | +
|
| 120 | + ```azurecli-interactive |
| 121 | + az acr cache list -r MyRegistry |
| 122 | + ``` |
| 123 | +
|
| 124 | +2. Run [az acr cache delete][az-acr-cache-delete] command to delete a cache rule. |
| 125 | +
|
| 126 | + - For example, to delete a cache rule for a given `MyRegistry` Azure Container Registry. |
| 127 | +
|
| 128 | + ```azurecli-interactive |
| 129 | + az acr cache delete -r MyRegistry -n MyRule |
| 130 | + ``` |
| 131 | +
|
| 132 | +3. Run[az acr credential set list][az-acr-credential-set-list] to list the credential sets in an Azure Container Registry. |
| 133 | +
|
| 134 | + - For example, to list the credential sets for a given `MyRegistry` Azure Container Registry. |
| 135 | +
|
| 136 | + ```azurecli-interactive |
| 137 | + az acr credential-set list -r MyRegistry |
| 138 | + ``` |
| 139 | +
|
| 140 | +4. Run [az-acr-credential-set-delete][az-acr-credential-set-delete] to delete a credential set. |
| 141 | +
|
| 142 | + - For example, to delete a credential set for a given `MyRegistry` Azure Container Registry. |
| 143 | +
|
| 144 | + ```azurecli-interactive |
| 145 | + az acr credential-set delete -r MyRegistry -n MyCredSet |
| 146 | + ``` |
| 147 | +
|
| 148 | +## Next steps |
| 149 | +
|
| 150 | +* Advance to the [next article](tutorial-troubleshoot-registry-cache.md) to walk through the troubleshoot guide for Registry Cache. |
| 151 | +
|
| 152 | +<!-- LINKS - External --> |
| 153 | +[create-and-store-keyvault-credentials]: ../key-vault/secrets/quick-create-cli.md#add-a-secret-to-key-vault |
| 154 | +[set-and-retrieve-a-secret]: ../key-vault/secrets/quick-create-cli.md#retrieve-a-secret-from-key-vault |
| 155 | +[az-keyvault-set-policy]: ../key-vault/general/assign-access-policy.md#assign-an-access-policy |
| 156 | +[Install Azure CLI]: /cli/azure/install-azure-cli |
| 157 | +[Azure Cloud Shell]: /azure/cloud-shell/quickstart |
| 158 | +[az-acr-cache-create]:/cli/azure/acr/cache#az-acr-cache-create |
| 159 | +[az-acr-cache-show]:/cli/azure/acr/cache#az-acr-cache-show |
| 160 | +[az-acr-cache-list]:/cli/azure/acr/cache#az-acr-cache-list |
| 161 | +[az-acr-cache-delete]:/cli/azure/acr/cache#az-acr-cache-delete |
| 162 | +[az-acr-cache-update]:/cli/azure/acr/cache#az-acr-cache-update |
| 163 | +[az-acr-credential-set-create]:/cli/azure/acr/credential-set#az-acr-credential-set-create |
| 164 | +[az-acr-credential-set-update]:/cli/azure/acr/credential-set#az-acr-credential-set-update |
| 165 | +[az-acr-credential-set-show]: /cli/azure/acr/credential-set#az-acr-credential-set-show |
| 166 | +[az-acr-credential-set-list]: /cli/azure/acr/credential-set#az-acr-credential-set-list |
| 167 | +[az-acr-credential-set-delete]: /cli/azure/acr/credential-set#az-acr-credential-set-delete |
0 commit comments