我有一个创建 ACR 注册表的 terraform 项目,我还使用 Docker 提供程序在本地构建节点项目,然后将其推送到 ACR。
运行 terraform init 并尝试应用更改后,我收到此错误:
Error: Error loading registry auth config: could not get auth config (the credentialhelper did not work or was not found): error getting credentials - err: no credentials server URL, out: `no credentials server URL`
│
│ with provider["registry.terraform.io/kreuzwerker/docker"],
│ on providers.tf line 29, in provider "docker":
│ 29: provider "docker" {
这是我当前的代码
resource "random_string" "random" {
length = 8
upper = false
special = false
}
locals {
acr_name = "acr${random_string.random.result}"
}
module "resource_group" {
source = "..//..//modules/resource_group"
name_prefix = var.resource_group_name_prefix
location = var.resource_group_location
}
resource "azurerm_container_registry" "acr" {
name = local.acr_name
location = module.resource_group.location
resource_group_name = module.resource_group.name
sku = "Premium"
}
data "azurerm_container_registry" "acr" {
name = azurerm_container_registry.acr.name
resource_group_name = module.resource_group.name
}
resource "docker_image" "image" {
name = "${data.azurerm_container_registry.acr.login_server}/my-app:v1"
build {
context = "./node-hello-world"
}
}
resource "docker_registry_image" "helloworld" {
name = docker_image.image.name
keep_remotely = true
}
我正在使用这个 Docker 提供商
docker = {
source = "kreuzwerker/docker"
version = "~>3.0.2"
}
并有我的授权信息
provider "docker" {
registry_auth {
address = data.azurerm_container_registry.acr.login_server
username = data.azurerm_container_registry.acr.admin_username
password = data.azurerm_container_registry.acr.admin_password
}
}
有谁知道为什么会这样吗?
Error: Error loading registry auth config: could not get auth config (the credentialhelper did not work or was not found): error getting credentials - err: no credentials server URL, out: `no credentials server URL`
导致上述错误的原因是未从 ACR 注册表中正确检索 ACR 登录服务器详细信息。您可以使用下面的代码在 Docker 提供程序中获取 ACR 登录服务器。
provider "docker" {
registry_auth {
address = "${azurerm_container_registry.acr.name}.azurecr.io"
username = azurerm_container_registry.acr.admin_username
password = azurerm_container_registry.acr.admin_password
}
}
如果 Docker 提供商遇到任何错误,您可以使用替代方法使用 Terraform 将映像推送到 ACR。
这里是更新的 Terraform 代码,用于将图像推送到 ACR
注意:就我而言,我使用公共图像进行测试,因为我没有自定义图像
provider "azurerm" {
features {}
subscription_id = "89a9"
}
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
azurerm = {
source = "hashicorp/azurerm"
version = "4.14.0"
}
}
}
resource "azurerm_resource_group" "rg" {
name = "${var.resource_group_name_prefix}-rg"
location = var.resource_group_location
lifecycle {
ignore_changes = all
}
}
resource "azurerm_container_registry" "acr" {
name = "venkatfacrtest"
location = azurerm_resource_group.rg.location
resource_group_name = azurerm_resource_group.rg.name
sku = "Premium"
admin_enabled = true
depends_on = [azurerm_resource_group.rg]
}
resource "docker_image" "nginx_image" {
name = "nginx:latest"
depends_on = [azurerm_container_registry.acr]
}
resource "null_resource" "dockerpush" {
provisioner "local-exec" {
command = <<-EOT
az acr login --name ${azurerm_container_registry.acr.name}
docker tag ${docker_image.nginx_image.image_id} ${azurerm_container_registry.acr.name}.azurecr.io/demoimage/nginx:v3 | tr -d '\r'
docker push ${azurerm_container_registry.acr.name}.azurecr.io/demoimage/nginx:v3 | tr -d '\r'
EOT
interpreter = ["bash", "-c"]
}
depends_on = [docker_image.nginx_image, azurerm_container_registry.acr]
}
地形适用
运行代码后,Docker 镜像以及标签已更改并成功推送到ACR
ACR 结果