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

Terraform推送镜像至ACR失败:无法获取认证配置求助

问题原因及解决方案

核心问题

出现该错误的主要原因有两个:

  1. ACR未启用管理员账户
    你的azurerm_container_registry资源未配置admin_enabled = true,默认情况下ACR的管理员账户是禁用状态,导致admin_username和admin_password为空值,无法完成认证流程。

  2. Docker Provider初始化时无法获取ACR属性
    Terraform在加载Provider阶段(早于资源创建流程)就会解析provider "docker"块中的配置,但此时ACR还未创建,data.azurerm_container_registry.acr无法获取有效数据,导致address参数为空,抛出"no credentials server URL"错误。

修复步骤

步骤1:启用ACR管理员账户

修改azurerm_container_registry资源,添加admin_enabled = true配置:

resource "azurerm_container_registry" "acr" {
  name                = local.acr_name
  location            = module.resource_group.location
  resource_group_name = module.resource_group.name
  sku                 = "Premium"
  admin_enabled       = true # 启用管理员账户,才能获取合法的用户名和密码
}

步骤2:调整Docker认证配置(二选一)

方案A:资源层面指定认证(推荐)

移除provider "docker"块中的全局registry_auth,改为在docker_registry_image资源中单独配置认证。这样认证信息会在ACR创建完成后再读取,避免初始化阶段的空值问题:

# 简化后的Docker Provider配置
provider "docker" {
  # 移除全局registry_auth配置
}

# 删除不必要的data资源,直接引用ACR resource属性
# 原data块可删除:
# data "azurerm_container_registry" "acr" {
#   name                = azurerm_container_registry.acr.name
#   resource_group_name = module.resource_group.name
# } 

# 修改docker_image的name引用
resource "docker_image" "image" {
  name = "${azurerm_container_registry.acr.login_server}/my-app:v1"
  build {
    context = "./node-hello-world"
  }
}

# 修改docker_registry_image资源,添加资源级别的认证
resource "docker_registry_image" "helloworld" {
  name          = docker_image.image.name
  keep_remotely = true

  registry_auth {
    address  = azurerm_container_registry.acr.login_server
    username = azurerm_container_registry.acr.admin_username
    password = azurerm_container_registry.acr.admin_password
  }
}

方案B:分阶段执行apply

如果不想修改代码结构,可以分两次执行terraform命令:

  1. 先创建ACR和资源组:
terraform apply -target=module.resource_group -target=azurerm_container_registry.acr
  1. 待ACR创建完成后,再执行完整的apply:
terraform apply

第二次apply时,ACR已存在,data.azurerm_container_registry.acr能正常获取到认证信息,Docker Provider初始化不会再报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 12:51:01