You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在容器应用配置中关联Azure.ResourceManager.AppContainers与ContainerRegistry

配置Azure容器应用与容器注册表的集成(Azure.ResourceManager SDK)

要将获取到的容器注册表关联到容器应用的配置中,你需要创建ContainerAppRegistryCredentials实例并添加到Registries集合里,具体实现如下:

修改后的完整代码示例

// Load the container registry
var registries = await arm.GetContainerRegistriesAsync();
// Select the first registry
var registry = registries.First();

// 获取注册表的管理员访问凭据
var registryCredentials = await registry.GetCredentialsAsync();

// Create a new container app configuration
var appConfig = new ContainerAppConfiguration()
{
    Ingress =
    {
        AllowInsecure = false,
        TargetPort = service.dockerProfile.InternalPort,
        ExposedPort = service.dockerProfile.ExternalPort,
        Transport = Azure.ResourceManager.AppContainers.Models.ContainerAppIngressTransportMethod.Http,
        External = service.dockerProfile.ExternalPort is not null
    },
    Registries =
    {
        new ContainerAppRegistryCredentials()
        {
            RegistryServer = registry.LoginServer,
            Username = registryCredentials.Value.Username,
            Password = registryCredentials.Value.Passwords.First(p => p.Name == "password").Value
        }
    }
};

var container = await arm.CreateContainerApp(service.containerAppName, new(Azure.Core.AzureLocation.SouthCentralUS)
{
    EnvironmentId = environment.Id,
    Configuration = appConfig
});

关键细节说明

  • RegistryServer:直接使用容器注册表对象的LoginServer属性,格式通常为{registry-name}.azurecr.io
  • 用户名与密码:通过registry.GetCredentialsAsync()获取注册表的管理员账户凭据,Passwords集合中第一个是主密码,第二个是辅助密码,可按需选择
  • 使用托管身份替代密钥(推荐):如果不想硬编码密码,可配置容器应用的托管身份,并为该身份赋予注册表的ACRPull权限,此时只需设置以下属性:
    new ContainerAppRegistryCredentials()
    {
        RegistryServer = registry.LoginServer,
        Identity = "/subscriptions/{sub-id}/resourceGroups/{rg-name}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identity-name}"
    };
    

内容的提问来源于stack exchange,提问作者ArcSpark76

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 03:46:07