Skip to content

Commit 3429afe

Browse files
authored
Merge pull request #203054 from alexandair/an-az-acr-auth-mi
Add PowerShell tab
2 parents 56c6f1c + 9d79440 commit 3429afe

File tree

1 file changed

+237
-19
lines changed

1 file changed

+237
-19
lines changed

articles/container-registry/container-registry-authentication-managed-identity.md

Lines changed: 237 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ For this article, you learn more about managed identities and how to:
1616
> * Grant the identity access to an Azure container registry
1717
> * Use the managed identity to access the registry and pull a container image
1818
19-
To create the Azure resources, this article requires that you run the Azure CLI version 2.0.55 or later. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][azure-cli].
19+
### [Azure CLI](#tab/azure-cli)
20+
21+
To create the Azure resources, this article requires that you run the Azure CLI version 2.0.55 or later. Run `az --version` to find the version. If you need to install or upgrade, see [Install Azure CLI][azure-cli-install].
22+
23+
### [Azure PowerShell](#tab/azure-powershell)
24+
25+
To create the Azure resources, this article requires that you run the Azure PowerShell module version 7.5.0 or later. Run `Get-Module Az -ListAvailable` to find the version. If you need to install or upgrade, see [Install Azure PowerShell module][azure-powershell-install].
26+
27+
---
2028

2129
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][docker-mac], [Windows][docker-windows], or [Linux][docker-linux] system.
2230

@@ -30,17 +38,29 @@ Then, use the identity to authenticate to any [service that supports Azure AD au
3038

3139
## Create a container registry
3240

41+
### [Azure CLI](#tab/azure-cli)
42+
3343
If you don't already have an Azure container registry, create a registry and push a sample container image to it. For steps, see [Quickstart: Create a private container registry using the Azure CLI](container-registry-get-started-azure-cli.md).
3444

3545
This article assumes you have the `aci-helloworld:v1` container image stored in your registry. The examples use a registry name of *myContainerRegistry*. Replace with your own registry and image names in later steps.
3646

47+
### [Azure PowerShell](#tab/azure-powershell)
48+
49+
If you don't already have an Azure container registry, create a registry and push a sample container image to it. For steps, see [Quickstart: Create a private container registry using Azure PowerShell](container-registry-get-started-powershell.md).
50+
51+
This article assumes you have the `aci-helloworld:v1` container image stored in your registry. The examples use a registry name of *myContainerRegistry*. Replace with your own registry and image names in later steps.
52+
53+
---
54+
3755
## Create a Docker-enabled VM
3856

39-
Create a Docker-enabled Ubuntu virtual machine. You also need to install the [Azure CLI](/cli/azure/install-azure-cli) on the virtual machine. If you already have an Azure virtual machine, skip this step to create the virtual machine.
57+
### [Azure CLI](#tab/azure-cli)
58+
59+
Create a Docker-enabled Ubuntu virtual machine. You also need to install the [Azure CLI][azure-cli-install] on the virtual machine. If you already have an Azure virtual machine, skip this step to create the virtual machine.
4060

4161
Deploy a default Ubuntu Azure virtual machine with [az vm create][az-vm-create]. The following example creates a VM named *myDockerVM* in an existing resource group named *myResourceGroup*:
4262

43-
```azurecli
63+
```azurecli-interactive
4464
az vm create \
4565
--resource-group myResourceGroup \
4666
--name myDockerVM \
@@ -51,6 +71,32 @@ az vm create \
5171

5272
It takes a few minutes for the VM to be created. When the command completes, take note of the `publicIpAddress` displayed by the Azure CLI. Use this address to make SSH connections to the VM.
5373

74+
### [Azure PowerShell](#tab/azure-powershell)
75+
76+
Create a Docker-enabled Ubuntu virtual machine. You also need to install the [Azure PowerShell][azure-powershell-install] on the virtual machine. If you already have an Azure virtual machine, skip this step to create the virtual machine.
77+
78+
Deploy a default Ubuntu Azure virtual machine with [New-AzVM][new-azvm]. The following example creates a VM named *myDockerVM* in an existing resource group named *myResourceGroup*. You will be prompted for a user name that will be used when you connect to the VM. Specify *azureuser* as the user name. You will also be asked for a password, which you can leave blank. Password login for the VM is disabled when using an SSH key.
79+
80+
```azurepowershell-interactive
81+
$vmParams = @{
82+
ResourceGroupName = 'MyResourceGroup'
83+
Name = 'myDockerVM'
84+
Image = 'UbuntuLTS'
85+
PublicIpAddressName = 'myPublicIP'
86+
GenerateSshKey = $true
87+
SshKeyName = 'mySSHKey'
88+
}
89+
New-AzVM @vmParams
90+
```
91+
92+
It takes a few minutes for the VM to be created. When the command completes, run the following command to get the public IP address. Use this address to make SSH connections to the VM.
93+
94+
```azurepowershell-interactive
95+
Get-AzPublicIpAddress -Name myPublicIP -ResourceGroupName myResourceGroup | Select-Object -ExpandProperty IpAddress
96+
```
97+
98+
---
99+
54100
### Install Docker on the VM
55101

56102
After the VM is running, make an SSH connection to the VM. Replace *publicIpAddress* with the public IP address of your VM.
@@ -72,33 +118,42 @@ After installation, run the following command to verify that Docker is running p
72118
sudo docker run -it mcr.microsoft.com/hello-world
73119
```
74120

75-
Output:
76-
77-
```
121+
```output
78122
Hello from Docker!
79123
This message shows that your installation appears to be working correctly.
80124
[...]
81125
```
126+
### [Azure CLI](#tab/azure-cli)
82127

83128
### Install the Azure CLI
84129

85130
Follow the steps in [Install Azure CLI with apt](/cli/azure/install-azure-cli-apt) to install the Azure CLI on your Ubuntu virtual machine. For this article, ensure that you install version 2.0.55 or later.
86131

132+
### [Azure PowerShell](#tab/azure-powershell)
133+
134+
### Install the Azure PowerShell
135+
136+
Follow the steps in [Installing PowerShell on Ubuntu][powershell-install] and [Install the Azure Az PowerShell module][azure-powershell-install] to install PowerShell and Azure PowerShell on your Ubuntu virtual machine. For this article, ensure that you install Azure PowerShell version 7.5.0 or later.
137+
138+
---
139+
87140
Exit the SSH session.
88141

89142
## Example 1: Access with a user-assigned identity
90143

91144
### Create an identity
92145

93-
Create an identity in your subscription using the [az identity create](/cli/azure/identity#az-identity-create) command. You can use the same resource group you used previously to create the container registry or virtual machine, or a different one.
146+
### [Azure CLI](#tab/azure-cli)
147+
148+
Create an identity in your subscription using the [az identity create][az-identity-create] command. You can use the same resource group you used previously to create the container registry or virtual machine, or a different one.
94149

95150
```azurecli-interactive
96151
az identity create --resource-group myResourceGroup --name myACRId
97152
```
98153

99-
To configure the identity in the following steps, use the [az identity show][az_identity_show] command to store the identity's resource ID and service principal ID in variables.
154+
To configure the identity in the following steps, use the [az identity show][az-identity-show] command to store the identity's resource ID and service principal ID in variables.
100155

101-
```azurecli
156+
```azurecli-interactive
102157
# Get resource ID of the user-assigned identity
103158
userID=$(az identity show --resource-group myResourceGroup --name myACRId --query id --output tsv)
104159
@@ -114,34 +169,99 @@ echo $userID
114169

115170
The ID is of the form:
116171

172+
```output
173+
/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId
174+
```
175+
176+
### [Azure PowerShell](#tab/azure-powershell)
177+
178+
Create an identity in your subscription using the [New-AzUserAssignedIdentity][new-azuserassignedidentity] cmdlet. You can use the same resource group you used previously to create the container registry or virtual machine, or a different one.
179+
180+
```azurepowershell-interactive
181+
New-AzUserAssignedIdentity -ResourceGroupName myResourceGroup -Location eastus -Name myACRId
117182
```
183+
184+
To configure the identity in the following steps, use the [Get-AzUserAssignedIdentity][get-azuserassignedidentity] cmdlet to store the identity's resource ID and service principal ID in variables.
185+
186+
```azurepowershell-interactive
187+
# Get resource ID of the user-assigned identity
188+
$userID = (Get-AzUserAssignedIdentity -ResourceGroupName myResourceGroup -Name myACRId).Id
189+
190+
# Get service principal ID of the user-assigned identity
191+
$spID = (Get-AzUserAssignedIdentity -ResourceGroupName myResourceGroup -Name myACRId).PrincipalId
192+
```
193+
194+
Because you need the identity's ID in a later step when you sign in to the Azure PowerShell from your virtual machine, show the value:
195+
196+
```azurepowershell-interactive
197+
$userID
198+
```
199+
200+
The ID is of the form:
201+
202+
```output
118203
/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId
119204
```
120205

206+
---
207+
121208
### Configure the VM with the identity
122209

210+
### [Azure CLI](#tab/azure-cli)
211+
123212
The following [az vm identity assign][az-vm-identity-assign] command configures your Docker VM with the user-assigned identity:
124213

125-
```azurecli
214+
```azurecli-interactive
126215
az vm identity assign --resource-group myResourceGroup --name myDockerVM --identities $userID
127216
```
128217

218+
### [Azure PowerShell](#tab/azure-powershell)
219+
220+
The following [Update-AzVM][update-azvm] command configures your Docker VM with the user-assigned identity:
221+
222+
```azurepowershell-interactive
223+
$vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myDockerVM
224+
Update-AzVM -ResourceGroupName myResourceGroup -VM $vm -IdentityType UserAssigned -IdentityID $userID
225+
```
226+
227+
---
228+
129229
### Grant identity access to the container registry
130230

231+
### [Azure CLI](#tab/azure-cli)
232+
131233
Now configure the identity to access your container registry. First use the [az acr show][az-acr-show] command to get the resource ID of the registry:
132234

133-
```azurecli
235+
```azurecli-interactive
134236
resourceID=$(az acr show --resource-group myResourceGroup --name myContainerRegistry --query id --output tsv)
135237
```
136238

137-
Use the [az role assignment create][az-role-assignment-create] command to assign the AcrPull role to the registry. This role provides [pull permissions](container-registry-roles.md) to the registry. To provide both pull and push permissions, assign the ACRPush role.
239+
Use the [az role assignment create][az-role-assignment-create] command to assign the AcrPull role to the identity. This role provides [pull permissions](container-registry-roles.md) to the registry. To provide both pull and push permissions, assign the AcrPush role.
138240

139-
```azurecli
241+
```azurecli-interactive
140242
az role assignment create --assignee $spID --scope $resourceID --role acrpull
141243
```
142244

245+
### [Azure PowerShell](#tab/azure-powershell)
246+
247+
Now configure the identity to access your container registry. First use the [Get-AzContainerRegistry][get-azcontainerregistry] command to get the resource ID of the registry:
248+
249+
```azurepowershell-interactive
250+
$resourceID = (Get-AzContainerRegistry -ResourceGroupName myResourceGroup -Name myContainerRegistry).Id
251+
```
252+
253+
Use the [New-AzRoleAssignment][new-azroleassignment] cmdlet to assign the AcrPull role to the identity. This role provides [pull permissions](container-registry-roles.md) to the registry. To provide both pull and push permissions, assign the AcrPush role.
254+
255+
```azurepowershell-interactive
256+
New-AzRoleAssignment -ObjectId $spID -Scope $resourceID -RoleDefinitionName AcrPull
257+
```
258+
259+
---
260+
143261
### Use the identity to access the registry
144262

263+
### [Azure CLI](#tab/azure-cli)
264+
145265
SSH into the Docker virtual machine that's configured with the identity. Run the following Azure CLI commands, using the Azure CLI installed on the VM.
146266

147267
First, authenticate to the Azure CLI with [az login][az-login], using the identity you configured on the VM. For `<userID>`, substitute the ID of the identity you retrieved in a previous step.
@@ -162,13 +282,40 @@ You should see a `Login succeeded` message. You can then run `docker` commands w
162282
docker pull mycontainerregistry.azurecr.io/aci-helloworld:v1
163283
```
164284

285+
### [Azure PowerShell](#tab/azure-powershell)
286+
287+
SSH into the Docker virtual machine that's configured with the identity. Run the following Azure PowerShell commands, using the Azure PowerShell installed on the VM.
288+
289+
First, authenticate to the Azure PowerShell with [Connect-AzAccount][connect-azaccount], using the identity you configured on the VM. For `-AccountId` specify a client ID of the identity.
290+
291+
```azurepowershell
292+
$clientId = (Get-AzUserAssignedIdentity -ResourceGroupName myResourceGroup -Name myACRId).ClientId
293+
Connect-AzAccount -Identity -AccountId $clientId
294+
```
295+
296+
Then, authenticate to the registry with [Connect-AzContainerRegistry][connect-azcontainerregistry]. When you use this command, the Azure PowerShell uses the Active Directory token created when you ran `Connect-AzAccount` to seamlessly authenticate your session with the container registry. (Depending on your VM's setup, you might need to run this command and docker commands with `sudo`.)
297+
298+
```azurepowershell
299+
sudo pwsh -command Connect-AzContainerRegistry -Name myContainerRegistry
300+
```
301+
302+
You should see a `Login succeeded` message. You can then run `docker` commands without providing credentials. For example, run [docker pull][docker-pull] to pull the `aci-helloworld:v1` image, specifying the login server name of your registry. The login server name consists of your container registry name (all lowercase) followed by `.azurecr.io` - for example, `mycontainerregistry.azurecr.io`.
303+
304+
```
305+
docker pull mycontainerregistry.azurecr.io/aci-helloworld:v1
306+
```
307+
308+
---
309+
165310
## Example 2: Access with a system-assigned identity
166311

167312
### Configure the VM with a system-managed identity
168313

314+
### [Azure CLI](#tab/azure-cli)
315+
169316
The following [az vm identity assign][az-vm-identity-assign] command configures your Docker VM with a system-assigned identity:
170317

171-
```azurecli
318+
```azurecli-interactive
172319
az vm identity assign --resource-group myResourceGroup --name myDockerVM
173320
```
174321

@@ -178,22 +325,59 @@ Use the [az vm show][az-vm-show] command to set a variable to the value of `prin
178325
spID=$(az vm show --resource-group myResourceGroup --name myDockerVM --query identity.principalId --out tsv)
179326
```
180327

328+
### [Azure PowerShell](#tab/azure-powershell)
329+
330+
The following [Update-AzVM][update-azvm] command configures your Docker VM with a system-assigned identity:
331+
332+
```azurepowershell-interactive
333+
$vm = Get-AzVM -ResourceGroupName myResourceGroup -Name myDockerVM
334+
Update-AzVM -ResourceGroupName myResourceGroup -VM $vm -IdentityType SystemAssigned
335+
```
336+
337+
Use the [Get-AzVM][get-azvm] command to set a variable to the value of `principalId` (the service principal ID) of the VM's identity, to use in later steps.
338+
339+
```azurepowershell-interactive
340+
$spID = (Get-AzVM -ResourceGroupName myResourceGroup -Name myDockerVM).Identity.PrincipalId
341+
```
342+
343+
---
344+
181345
### Grant identity access to the container registry
182346

347+
### [Azure CLI](#tab/azure-cli)
348+
183349
Now configure the identity to access your container registry. First use the [az acr show][az-acr-show] command to get the resource ID of the registry:
184350

185-
```azurecli
351+
```azurecli-interactive
186352
resourceID=$(az acr show --resource-group myResourceGroup --name myContainerRegistry --query id --output tsv)
187353
```
188354

189-
Use the [az role assignment create][az-role-assignment-create] command to assign the AcrPull role to the identity. This role provides [pull permissions](container-registry-roles.md) to the registry. To provide both pull and push permissions, assign the ACRPush role.
355+
Use the [az role assignment create][az-role-assignment-create] command to assign the AcrPull role to the identity. This role provides [pull permissions](container-registry-roles.md) to the registry. To provide both pull and push permissions, assign the AcrPush role.
190356

191-
```azurecli
357+
```azurecli-interactive
192358
az role assignment create --assignee $spID --scope $resourceID --role acrpull
193359
```
194360

361+
### [Azure PowerShell](#tab/azure-powershell)
362+
363+
Now configure the identity to access your container registry. First use the [[Get-AzContainerRegistry][get-azcontainerregistry] command to get the resource ID of the registry:
364+
365+
```azurepowershell-interactive
366+
$resourceID = (Get-AzContainerRegistry -ResourceGroupName myResourceGroup -Name myContainerRegistry).Id
367+
```
368+
369+
Use the [New-AzRoleAssignment][new-azroleassignment] cmdlet to assign the AcrPull role to the identity. This role provides [pull permissions](container-registry-roles.md) to the registry. To provide both pull and push permissions, assign the AcrPush role.
370+
371+
```azurepowershell-interactive
372+
New-AzRoleAssignment -ObjectId $spID -Scope $resourceID -RoleDefinitionName AcrPull
373+
```
374+
375+
---
376+
195377
### Use the identity to access the registry
196378

379+
### [Azure CLI](#tab/azure-cli)
380+
197381
SSH into the Docker virtual machine that's configured with the identity. Run the following Azure CLI commands, using the Azure CLI installed on the VM.
198382

199383
First, authenticate the Azure CLI with [az login][az-login], using the system-assigned identity on the VM.
@@ -213,6 +397,29 @@ You should see a `Login succeeded` message. You can then run `docker` commands w
213397
```
214398
docker pull mycontainerregistry.azurecr.io/aci-helloworld:v1
215399
```
400+
### [Azure PowerShell](#tab/azure-powershell)
401+
402+
SSH into the Docker virtual machine that's configured with the identity. Run the following Azure PowerShell commands, using the Azure PowerShell installed on the VM.
403+
404+
First, authenticate the Azure PowerShell with [Connect-AzAccount][connect-azaccount], using the system-assigned identity on the VM.
405+
406+
```azurepowershell
407+
Connect-AzAccount -Identity
408+
```
409+
410+
Then, authenticate to the registry with [Connect-AzContainerRegistry][connect-azcontainerregistry]. When you use this command, the PowerShell uses the Active Directory token created when you ran `Connect-AzAccount` to seamlessly authenticate your session with the container registry. (Depending on your VM's setup, you might need to run this command and docker commands with `sudo`.)
411+
412+
```azurepowershell
413+
sudo pwsh -command Connect-AzContainerRegistry -Name myContainerRegistry
414+
```
415+
416+
You should see a `Login succeeded` message. You can then run `docker` commands without providing credentials. For example, run [docker pull][docker-pull] to pull the `aci-helloworld:v1` image, specifying the login server name of your registry. The login server name consists of your container registry name (all lowercase) followed by `.azurecr.io` - for example, `mycontainerregistry.azurecr.io`.
417+
418+
```
419+
docker pull mycontainerregistry.azurecr.io/aci-helloworld:v1
420+
```
421+
422+
---
216423

217424
## Next steps
218425

@@ -236,12 +443,23 @@ In this article, you learned about using managed identities with Azure Container
236443

237444
<!-- LINKS - Internal -->
238445
[az-login]: /cli/azure/reference-index#az_login
446+
[connect-azaccount]: /powershell/module/az.accounts/connect-azaccount
239447
[az-acr-login]: /cli/azure/acr#az_acr_login
448+
[connect-azcontainerregistry]: /powershell/module/az.containerregistry/connect-azcontainerregistry
240449
[az-acr-show]: /cli/azure/acr#az_acr_show
450+
[get-azcontainerregistry]: /powershell/module/az.containerregistry/get-azcontainerregistry
241451
[az-vm-create]: /cli/azure/vm#az_vm_create
452+
[new-azvm]: /powershell/module/az.compute/new-azvm
242453
[az-vm-show]: /cli/azure/vm#az_vm_show
454+
[get-azvm]: /powershell/module/az.compute/get-azvm
455+
[az-identity-create]: /cli/azure/identity#az_identity_create
456+
[new-azuserassignedidentity]: /powershell/module/az.managedserviceidentity/new-azuserassignedidentity
243457
[az-vm-identity-assign]: /cli/azure/vm/identity#az_vm_identity_assign
458+
[update-azvm]: /powershell/module/az.compute/update-azvm
244459
[az-role-assignment-create]: /cli/azure/role/assignment#az_role_assignment_create
245-
[az-acr-login]: /cli/azure/acr#az_acr_login
460+
[new-azroleassignment]: /powershell/module/az.resources/new-azroleassignment
246461
[az-identity-show]: /cli/azure/identity#az_identity_show
247-
[azure-cli]: /cli/azure/install-azure-cli
462+
[get-azuserassignedidentity]: /powershell/module/az.managedserviceidentity/get-azuserassignedidentity
463+
[azure-cli-install]: /cli/azure/install-azure-cli
464+
[azure-powershell-install]: /powershell/azure/install-az-ps
465+
[powershell-install]: /powershell/scripting/install/install-ubuntu

0 commit comments

Comments
 (0)