docker - docker 容器-缺少属性

标签 docker terraform

我需要安装了docker的自定义centos镜像。因此,我使用centos镜像构建了该镜像并对其进行了标记(如下所示)。
$ docker image ls

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              custom              84766562f881        4 hours ago         664MB
centos/systemd      latest              05d3c1e2d0c1        7 weeks ago         202MB

我正在尝试在本地计算机上使用Terraform部署几个容器,每个容器都有一个来自另一个文件的唯一名称。 docker镜像位于本地计算机上。这是TF代码。
$ cat main.tf
provider "docker" {

}
resource "docker_image" "centos" {
  name = "centos:custom"
}
resource "docker_container" "app_swarm" {
  image = "${docker_image.centos.custom}"
  count = "${length(var.docker_cont)}"
  name = "${element(var.docker_cont, count.index)}"
}

当我运行terraform apply时,出现此错误,我不确定该如何解决。有人可以指出我正确的方向吗?
Error: Error running plan: 1 error(s) occurred:
* docker_container.app_swarm: 3 error(s) occurred:
* docker_container.app_swarm[0]: Resource 'docker_image.centos' does not have attribute 'custom' for variable 'docker_image.centos.custom'
* docker_container.app_swarm[1]: Resource 'docker_image.centos' does not have attribute 'custom' for variable 'docker_image.centos.custom'
* docker_container.app_swarm[2]: Resource 'docker_image.centos' does not have attribute 'custom' for variable 'docker_image.centos.custom'

是的,另一个文件存在名称,它是一个简单列表。

编辑:

大卫,谢谢您,尝试了您的建议并修改了代码,使其看起来像-
provider "docker" {

}
resource "docker_image" "centos" {
  name = "centos:custom"
}
resource "docker_container" "app_swarm" {
  image = "${docker_image.centos.latest}"
  count = "${length(var.docker_cont)}"
  name = "${element(var.docker_cont, count.index)}"
}

但是现在我得到了这个错误。
Error: Error applying plan:

1 error(s) occurred:

* docker_image.centos: 1 error(s) occurred:
* docker_image.centos: Unable to read Docker image into resource: Unable to pull image centos:custom: error pulling image centos:custom: Error response from daemon: manifest for centos:custom not found

我想我必须设置一个本地Docker存储库才能使它正常工作,但是我不确定吗?

最佳答案

您只能使用docker_image插值中the ${docker_image.centos...} resource documentation中列出的特定字段。特别是,即使您不使用标记:latest,也需要.latest属性引用:

image = "${docker_image.centos.latest}"

(如果该图像实际上是您在本地构建的,则可能还需要在keep_locally资源上指定 docker_image 选项,以便terraform destroy不会将其删除。)

关于docker - docker 容器-缺少属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54818788/

相关文章:

amazon-web-services - 使用 Terraform 将文件传递给新创建的 ec2 实例而不共享 "connection"部分中的私钥

continuous-integration - 使用 Terraform 时的最佳实践

docker - Docker使用FROM获得先前的镜像

docker - 运行 Celery 任务的 Docker 容器的 HEALTHCHECK?

amazon-web-services - Terraform ELB S3 权限问题

terraform - 资源产出值从一个计划转移到另一计划

jenkins - Terraform aws_cloudformation_stack AlreadyExistsException

docker - Docker构建返回非零代码:1 microsoft/dotnet

docker - 从 Docker 容器获取 Mac 地址

docker - 在容器中重新打开项目的问题