|
| 1 | +--- |
| 2 | +title: Azure Container Registry integration |
| 3 | +description: Learn how to integrate Azure Container Registry with .NET Aspire for secure container image management. |
| 4 | +ms.date: 05/09/2025 |
| 5 | +--- |
| 6 | + |
| 7 | +# .NET Aspire Azure Container Registry integration |
| 8 | + |
| 9 | +[!INCLUDE [includes-hosting](../includes/includes-hosting.md)] |
| 10 | + |
| 11 | +[Azure Container Registry (ACR)](/azure/container-registry) is a managed Docker container registry service that simplifies the storage, management, and deployment of container images. The .NET Aspire integration allows you to provision or reference an existing Azure Container Registry and seamlessly integrate it with your app's compute environments. |
| 12 | + |
| 13 | +## Overview |
| 14 | + |
| 15 | +.NET Aspire apps often build and run container images locally but require secure registries for staging and production environments. The Azure Container Registry integration provides the following capabilities: |
| 16 | + |
| 17 | +- Provision or reference an existing Azure Container Registry. |
| 18 | +- Attach the registry to any compute-environment resource (for example, Azure Container Apps, Docker, Kubernetes) to ensure proper credential flow. |
| 19 | +- Grant fine-grained ACR role assignments to other Azure resources. |
| 20 | + |
| 21 | +## Supported scenarios |
| 22 | + |
| 23 | +The Azure Container Registry integration supports the following scenarios: |
| 24 | + |
| 25 | +- **Provisioning a new registry**: Automatically create a new Azure Container Registry for your app. |
| 26 | +- **Referencing an existing registry**: Use an existing Azure Container Registry by providing its name and resource group. |
| 27 | +- **Credential management**: Automatically flow credentials to compute environments for secure image pulls. |
| 28 | +- **Role assignments**: Assign specific roles (for example, `AcrPush`) to enable services to push images to the registry. |
| 29 | + |
| 30 | +## Hosting integration |
| 31 | + |
| 32 | +The Azure Container Registry integration is part of the .NET Aspire hosting model. It allows you to define and manage your app's resources in a declarative manner. The integration is available in the [📦 Aspire.Hosting.Azure.ContainerRegistry](https://www.nuget.org/packages/Aspire.Hosting.Azure.ContainerRegistry) NuGet package. |
| 33 | + |
| 34 | +### [.NET CLI](#tab/dotnet-cli) |
| 35 | + |
| 36 | +```dotnetcli |
| 37 | +dotnet add package Aspire.Hosting.Azure.ContainerRegistry |
| 38 | +``` |
| 39 | + |
| 40 | +### [PackageReference](#tab/package-reference) |
| 41 | + |
| 42 | +```xml |
| 43 | +<PackageReference Include="Aspire.Hosting.Azure.ContainerRegistry" |
| 44 | + Version="*" /> |
| 45 | +``` |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +For more information, see [dotnet add package](/dotnet/core/tools/dotnet-add-package) or [Manage package dependencies in .NET applications](/dotnet/core/tools/dependencies). |
| 50 | + |
| 51 | +### Provision a new container registry |
| 52 | + |
| 53 | +The following example demonstrates how to provision a new Azure Container Registry and attach it to a container app environment: |
| 54 | + |
| 55 | +:::code source="snippets/acr/AspireAcr.AppHost/Program.cs"::: |
| 56 | + |
| 57 | +The preceding code: |
| 58 | + |
| 59 | +- Creates a new Azure Container Registry named `my-acr`. |
| 60 | +- Attaches the registry to an Azure Container Apps environment named `env`. |
| 61 | +- Optionally grants the `AcrPush` role to a project resource named `api`, allowing it to push images to the registry. |
| 62 | + |
| 63 | +For more information, see [Configure Azure Container Apps environments](configure-aca-environments.md). |
| 64 | + |
| 65 | +> [!IMPORTANT] |
| 66 | +> When you call `AddAzureContainerRegistry` or `AddAzureContainerAppEnvironment`, they implicitly call the idempotent <xref:Aspire.Hosting.AzureProvisionerExtensions.AddAzureProvisioning*>—which adds support for generating Azure resources dynamically during app startup. The app must configure the appropriate subscription and location. For more information, see [Local provisioning: Configuration](local-provisioning.md#configuration). |
| 67 | +
|
| 68 | +#### Provisioning-generated Bicep |
| 69 | + |
| 70 | +If you're new to [Bicep](/azure/azure-resource-manager/bicep/overview), it's a domain-specific language for defining Azure resources. With .NET Aspire, you don't need to write Bicep by-hand, instead the provisioning APIs generate Bicep for you. When you publish your app, the generated Bicep is output alongside the manifest file. When you add an Azure Container Registry resource, the following Bicep is generated: |
| 71 | + |
| 72 | +:::code language="bicep" source="snippets/acr/AspireAcr.AppHost/AspireAcr.AppHost/my-acr.module.bicep"::: |
| 73 | + |
| 74 | +The preceding Bicep provisions an Azure Container Registry resource. Additionally, the added Azure Container App environment resource is also generated: |
| 75 | + |
| 76 | +:::code language="bicep" source="snippets/acr/AspireAcr.AppHost/AspireAcr.AppHost/env.module.bicep"::: |
| 77 | + |
| 78 | +The generated Bicep is a starting point and is influenced by changes to the provisioning infrastructure in C#. Customizations to the Bicep file directly are overwritten, so make changes through the C# provisioning APIs to ensure they're reflected in the generated files. |
| 79 | + |
| 80 | +### Reference an existing container registry |
| 81 | + |
| 82 | +To reference an existing Azure Container Registry, use the <xref:Aspire.Hosting.ExistingAzureResourceExtensions.PublishAsExisting*> method with the registry name and resource group: |
| 83 | + |
| 84 | +:::code source="snippets/acr/AspireAcr.AppHost/Program.Existing.cs" id="existing"::: |
| 85 | + |
| 86 | +The preceding code: |
| 87 | + |
| 88 | +- References an existing Azure Container Registry named `my-acr` in the specified resource group. |
| 89 | +- Attaches the registry to an Azure Container Apps environment named `env`. |
| 90 | +- Uses parameters to allow for dynamic configuration of the registry name and resource group. |
| 91 | +- Optionally grants the `AcrPush` role to a project resource named `api`, allowing it to push images to the registry. |
| 92 | + |
| 93 | +### Key features |
| 94 | + |
| 95 | +**Automatic credential flow** |
| 96 | + |
| 97 | +When you attach an Azure Container Registry to a compute environment, Aspire automatically ensures that the correct credentials are available for secure image pulls. |
| 98 | + |
| 99 | +**Fine-grained role assignments** |
| 100 | + |
| 101 | +You can assign specific roles to Azure resources to control access to the container registry. For example, the AcrPush role allows a service to push images to the registry. |
| 102 | + |
| 103 | +```csharp |
| 104 | +builder.AddProject("api", "../Api/Api.csproj") |
| 105 | + .WithRoleAssignments(acr, ContainerRegistryBuiltInRole.AcrPush); |
| 106 | +``` |
| 107 | + |
| 108 | +## See also |
| 109 | + |
| 110 | +- [Azure Container Registry documentation](/azure/container-registry) |
| 111 | +- [.NET Aspire Azure integrations overview](integrations-overview.md) |
0 commit comments