docker - 为什么在使用多个 Docker 主机时出现 Unable to create container with image Unable to pull image 错误 pulling image?

标签 docker terraform

我正在尝试使用 terraform (0.12.24) 和多个 Docker 提供程序(插件版本 2.7.0)执行简单部署。我使用下面的 Terraform 模板的目的是将两个不同的容器部署到两个不同的支持 Docker 的主机。

# Configure the Docker provider
provider "docker" {
  host = "tcp://192.168.1.10:2375/"
}

provider "docker" {
  alias = "worker"
  host = "tcp://127.0.0.1:2375/"
}

# Create a container
resource "docker_container" "hello" {
  image = docker_image.world.latest
  name  = "hello"
}

resource "docker_container" "test" {
  provider = docker.worker
  image = docker_image.world.latest
  name  = "test"
}

resource "docker_image" "world" {
  name = "hello-world:latest"
}

docker 命令在没有 root 权限的情况下成功运行。机器 192.168.1.10 和 127.0.0.1 的 Docker 守护进程都在 2375 上监听,可以从主机访问并且可以响应使用 curl 执行的直接 Docker REST API 调用(创建、拉取等)。手动拉取镜像也适用于两台主机,我这样做是为了确保最新的 hello-world 镜像存在于两台主机中。

但是,terraform 部署 (terraform apply) 失败并出现以下错误:

docker_container.hello: Creating...
docker_container.test: Creating...
docker_container.hello: Creation complete after 1s [id=77e515b4269aed255d4becac61f40d38e09838cdf8285294bf51f3c7cddbf2bf]

Error: Unable to create container with image sha256:a29f45ccde2ac0bde957b1277b1501f471960c8ca49f1588c6c885941640ae60: Unable to pull image sha256:a29f45ccde2ac0bde957b1277b1501f471960c8ca49f1588c6c885941640ae60: error pulling image sha256:a29f45ccde2ac0bde957b1277b1501f471960c8ca49f1588c6c885941640ae60: Error response from daemon: pull access denied for sha256, repository does not exist or may require 'docker login'

  on test.tf line 17, in resource "docker_container" "test":
  17: resource "docker_container" "test" {

为什么在使用多个 Docker 主机时出现 Unable to create container with image Unable to pull image 错误 pulling image?

最佳答案

docker_container.test 引用 docker_image.world,但它们使用不同的提供程序(defaultdocker.worker ):

resource "docker_container" "test" {
  provider = docker.worker
  image = docker_image.world.latest
  name  = "test"
}

resource "docker_image" "world" {
  name = "hello-world:latest"
}

这是致命的,因为 docker_image.world 使用提供的 defaulttcp://192.168.1.10 上运行 docker pull :2375/(不在 tcp://127.0.0.1:2375/)。

这可以通过使用提供商 docker.world_worker 创建 docker_image 来匹配 docker_container.test 来解决,如下所示:

resource "docker_container" "test" {
  provider = docker.world_worker
  image = docker_image.world.latest
  name  = "test"
}

resource "docker_image" "world_worker" {
  provider = docker.world_worker
  name = "hello-world:latest"
}

关于docker - 为什么在使用多个 Docker 主机时出现 Unable to create container with image Unable to pull image 错误 pulling image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61806488/

相关文章:

docker - 链接和主机名docker compose

azure - 地形错误 : Failed to query available provider packages

azure - Terraform 在未指定的情况下强制将值设置为 null

node.js - 如何动态更改在 Docker 容器中运行的 React 应用程序中的 API URL,而无需重建?

docker - 如何在docker中具有相同端口的同一本地主机中运行两个Web应用程序?

r - 对R中使用mxnet软件包的应用进行Docker化

amazon-web-services - 从VPC Terraform社区模块获取subnet_id

azure - Terraform - 在调用中使用 for_each 时从模块输出中检索特定属性

Powershell 配置文件将参数附加到某个命令

docker - 删除docker镜像后docker-compose失败