Terraform docker无法通过容器注册表对远程主机进行身份验证

时间:2020-11-04 08:28:35

标签: docker terraform terraform-provider-docker

我正在使用Terraform 0.13.4的Windows计算机上,并尝试使用Terraform和Docker提供程序在远程主机上启动一些容器:

provider "docker" {
  host = "tcp://myvm:2376/"

  registry_auth {
    address = "myregistry:443"
    username = "myusername"
    password = "mypassword"
  }

  ca_material = file(pathexpand(".docker/ca.pem"))
  cert_material = file(pathexpand(".docker/cert.pem"))
  key_material = file(pathexpand(".docker/key.pem"))
}

data "docker_registry_image" "mycontainer" {
  name = "myregistry:443/lvl1/lvl2/myimage:latest"
}

我对此很难,因为它无法通过我的私人注册表进行身份验证。总是得到401 Unauthorized

如果我不这样做来获取sha256_digest并仅使用docker_container资源,则一切正常,但会强制替换正在运行的容器。

2 个答案:

答案 0 :(得分:1)

您好Angelos,如果您不想强制替换正在运行的容器,则应尝试以下操作:

provider "docker" {
  host = "tcp://myvm:2376/"

  registry_auth {
    address = "myregistry:443"
    username = "myusername"
    password = "mypassword"
  }

  ca_material = file(pathexpand(".docker/ca.pem"))
  cert_material = file(pathexpand(".docker/cert.pem"))
  key_material = file(pathexpand(".docker/key.pem"))
}
data "docker_registry_image" "mycontainer" {
  name = "myregistry:443/lvl1/lvl2/myimage:latest"
}

resource "docker_image" "example" {
  name = data.docker_registry_image.mycontainer.name
  pull_triggers = [data.docker_registry_image.mycontainer.sha256_digest]
  keep_locally = true
}

然后在容器中使用:

resource "docker_container" "example" {
  image = docker_image.example.latest
  name = "container_name"
   
}

您应该使用

docker_image.example.latest

使用资源docker_image本身(如果已经存在),他将不会拉取图像并且不会重新启动容器,但是如果您将名称作为字符串传递,他将每次都替换容器。

https://www.terraform.io/docs/providers/docker/r/container.html

答案 1 :(得分:1)

事实证明该代码正确,并且我正在使用的容器服务(ProGet的较旧版本)未正确响应auth调用。我使用另一个注册表测试了代码,一切正常。