1

我正在关注terraform.io 上的教程,该教程让我使用 terraform 配置 docker 映像和容器,然后销毁 terraform 堆栈。但是,我收到以下错误:

Error: Unable to remove Docker image: 
Error response from daemon: conflict: unable to delete 540a289bab6c (must be forced) - 
image is being used by stopped container ae12197d265d

我知道本机 Docker 解决方案正在运行docker rmi -f 540a289bab6c。但是,我想知道是否有一种 terraform 方法来解决这个问题?

terraform 资源的文档docker_image显示了 terraform 试图破坏图像的原因terraform destroy:模板main.tfkeep-locally设置为true. 但它没有说明如何强制进行这种破坏。

教程的main.tf内容如下:

terraform {
  required_providers {
    docker = {
      source = "terraform-providers/docker"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}

那么,如何terraform destroy在不借助 docker 原生工具的手动干预的情况下强制使用此模板呢?

4

1 回答 1

1

错误消息表明有另一个容器依赖于相同的图像。可能是在 terraform 之外配置了一个单独的 docker 容器,并在教程中使用了相同的 nginx docker 映像。检查您docker ps -a是否有这样的容器,如果有,只需运行docker rm -f <container_name>以将其删除,您的 terraform destroy 应该可以工作。

于 2020-10-25T18:08:41.653 回答