Azure Container Registry (ACR) は、プライベート Docker コンテナー イメージを格納するために使用される、Azure ベースの管理されたコンテナー レジストリ サービスです。 この記事では、Azure Container Instances を使用してコンテナー グループにデプロイするときに、Azure コンテナー レジストリに格納されているコンテナー イメージをプルする方法について説明します。 レジストリ アクセスを構成する 1 つの方法は、Microsoft Entra マネージド ID を作成することです。
Azure Container Registry (ACR) へのアクセスがプライベート エンドポイントを使用して制限されている場合、マネージド ID を使用すると、Azure Container Instances が仮想ネットワークにデプロイされて、そのプライベート エンドポイント経由でコンテナー レジストリにアクセスできるようになります。
前提条件
Azure コンテナー レジストリ: 少なくとも1つのイメージを含む Premium SKU Azure コンテナー レジストリ が必要です。 レジストリを作成する必要がある場合は、「Azure CLI を使用したコンテナー レジストリの作成」を参照してください。 レジストリの id
と loginServer
をメモしておいてください
Azure CLI:この記事のコマンド ラインの例は Azure CLI を使用し、Bash シェル用にフォーマットされています。 ローカルに Azure CLI をインストールするほかに、Azure Cloud Shell を使用することもできます。
制限事項
- Windows コンテナーでは、ACR を使用したシステム割り当てマネージド ID 認証イメージ プルはサポートされていません。ユーザー割り当てのみです。
レジストリの認証を構成する
コンテナー レジストリでは、信頼されたサービス (Trusted Services)が有効になっている必要があります。 信頼されたサービスを有効にする方法については、「 信頼されたサービスがネットワークで制限されたコンテナー レジストリに安全にアクセスできるようにする」を参照してください。
ID の作成
az identity create コマンドを使用して、サブスクリプション内に ID を作成します。 コンテナー レジストリの作成には、以前使ったものと同じリソース グループ、または別のリソース グループを使用できます。
az identity create --resource-group myResourceGroup --name myACRId
後の手順で ID を構成するために、az identity show コマンドを使用して、ID のリソース ID とサービス プリンシパル ID を変数に格納します。
今後の手順で ID を適切に構成するには、 az identity show を使用して、ID のうちリソース ID とサービス プリンシパル ID を取得し、変数に格納します。
# 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)
仮想マシンから CLI にサインインするには、ID のうちリソース ID が必要になります。 値を表示するには:
echo $USERID
リソース ID の形式は次のとおりです:
/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId
また、マネージド ID にコンテナー レジストリへのアクセスを許可するために、サービス プリンシパル ID も必要になります。 値を表示するには:
echo $SPID
サービス プリンシパル ID の形式は次のとおりです:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx
ID にロールの割り当てを許可する
ID からコンテナー レジストリへにアクセスするため、ID にロールの割り当てを許可する必要があります。 次のコマンドを使用して、先ほど作成した ID に acrpull
ロールの許可をします。レジストリの ID と、以前取得したサービス プリンシパルを必ず指定してください。
az role assignment create --assignee $SPID --scope <registry-id> --role acrpull
Azure Resource Manager (ARM) テンプレートを使用したデプロイ
まず、次の JSON を azuredeploy.json
という名前の新しいファイルにコピーします。 Azure Cloud Shell では、Visual Studio Code を使用して作業ディレクトリにファイルを作成できます。
code azuredeploy.json
imageRegistryCredentials
プロパティをコンテナー グループ定義に含めることで、ARM テンプレート内に Azure Container Registry のプロパティを指定できます。 たとえば、レジストリの資格情報を直接指定できます。
注意
これは包括的な ARM テンプレートではなく、完全なテンプレートの resources
セクションが どのようになるかの例です。
{
"type": "Microsoft.ContainerInstance/containerGroups",
"apiVersion": "2021-09-01",
"name": "myContainerGroup",
"___location": "norwayeast",
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId": {}
}
},
"properties": {
"containers": [
{
"name": "mycontainer",
"properties": {
"image": "myacr.azurecr.io/hello-world:latest",
"ports": [
{
"port": 80,
"protocol": "TCP"
}
],
"resources": {
"requests": {
"cpu": 1,
"memoryInGB": 1
}
}
}
}
],
"imageRegistryCredentials": [
{
"server":"myacr.azurecr.io",
"identity":"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId"
}
],
"ipAddress": {
"ports": [
{
"port": 80,
"protocol": "TCP"
}
],
"type": "public"
},
"osType": "Linux"
}
}
テンプレートのデプロイ
次のコマンドを使用して、Resource Manager テンプレートをデプロイします:
az deployment group create --resource-group myResourceGroup --template-file azuredeploy.json
Azure CLI を使用したデプロイ
Azure CLI 経由でイメージのプルを認証するマネージド ID を使用してコンテナー グループをデプロイするには、次のコマンドを使用して、 <dns-label>
がグローバルに一意であることを確認します:
az container create --name my-containergroup --resource-group myResourceGroup --image <loginServer>/hello-world:v1 --acr-identity $USERID --assign-identity $USERID --ports 80 --dns-name-label <dns-label>
Azure CLI を使用して仮想ネットワークにデプロイする
マネージド ID を使用して仮想ネットワークにコンテナー グループをデプロイし、Azure CLI を介してプライベート エンドポイントの背後で実行される ACR からイメージ プルを認証するには、次のコマンドを使用します。
az container create --name my-containergroup --resource-group myResourceGroup --image <loginServer>/hello-world:v1 --acr-identity $USERID --assign-identity $USERID --vnet "/subscriptions/$SUBSCRIPTION_ID/resourceGroups/"/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myVNetResourceGroup/providers/ --subnet mySubnetName
仮想ネットワークにデプロイする方法の詳細については、「コンテナー インスタンスを Azure 仮想ネットワークにデプロイする」を参照してください。
YAML と Azure CLI を使用して仮想ネットワークにマルチコンテナー グループをデプロイする
マネージド ID を使用して仮想ネットワークにマルチコンテナー グループをデプロイし、Azure CLI を介してプライベート エンドポイントの背後で実行される ACR からのイメージ プルを認証するには、YAML ファイルにコンテナー グループの構成を指定するのがよいでしょう。 その後、YAML ファイルをパラメーターとしてコマンドに渡します。
apiVersion: '2021-10-01'
___location: eastus
type: Microsoft.ContainerInstance/containerGroups
identity:
type: UserAssigned
userAssignedIdentities: {
'/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId': {}
}
properties:
osType: Linux
imageRegistryCredentials:
- server: myacr.azurecr.io
identity: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/myACRId'
subnetIds:
- id: '/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx/resourceGroups/myVNetResourceGroup/providers/Microsoft.Network/virtualNetworks/myVNetName/subnets/mySubnetName'
name: mySubnetName
containers:
- name: myContainer-1
properties:
resources:
requests:
cpu: '.4'
memoryInGb: '1'
environmentVariables:
- name: CONTAINER
value: 1
image: 'myacr.azurecr.io/myimage:latest'
- name: myContainer-2
properties:
resources:
requests:
cpu: '.4'
memoryInGb: '1'
environmentVariables:
- name: CONTAINER
value: 2
image: 'myacr.azurecr.io/myimage:latest'
az container create --name my-containergroup --resource-group myResourceGroup --file my-YAML-file.yaml
マルチコンテナー グループにデプロイする方法の詳細については、マルチコンテナー グループのデプロイに関する記事を参照してください。
リソースをクリーンアップする
Azure サブスクリプションからすべてのリソースを削除するには、リソース グループを削除します:
az group delete --name myResourceGroup