如何让 Terraform 在 Docker 容器之前启动 Docker 网络

How to get Terraform to start Docker Network before Docker Containers

Terraform Apply 的第一个 运行 给出“错误:无法启动容器:来自守护程序的错误响应:未找到网络疫苗网络”

第二个运行:一切正常。

我认为 Terraform 会自行管理依赖关系 - 显然它在这里失败了。我的文件有错误吗?或者我可以自己做依赖吗?

.tf 文件:

# Setting up docker network
resource "docker_network" "private_network" {
  name       = "vaccine-net"
  attachable = true
  ipam_config {
    subnet  = "10.0.1.0/24"
    gateway = "10.0.1.254"
  }
}

# create db container
resource "docker_container" "bootstrapper" {
  name  = "bootstrapper"
  image = "vaccine/bootstrapper"
  networks_advanced {
    name = "vaccine-net"
    ipv4_address ="10.0.1.1"
  }
}

你确定你的网络是在创建容器之前创建的吗?我在您的网络和容器之间看不到 link。参考docker网络试试(首选):

# Setting up docker network
resource "docker_network" "private_network" {
  name       = "vaccine-net"
  attachable = true
  ipam_config {
    subnet  = "10.0.1.0/24"
    gateway = "10.0.1.254"
  }
}

# create db container
resource "docker_container" "bootstrapper" {
  name  = "bootstrapper"
  image = "vaccine/bootstrapper"
  networks_advanced {
    name = "${docker_network.private_network.name}"
    ipv4_address ="10.0.1.1"
  }
}

或明确地 depends_on:

# Setting up docker network
resource "docker_network" "private_network" {
  name       = "vaccine-net"
  attachable = true
  ipam_config {
    subnet  = "10.0.1.0/24"
    gateway = "10.0.1.254"
  }
}

# create db container
resource "docker_container" "bootstrapper" {
  name  = "bootstrapper"
  image = "vaccine/bootstrapper"
  networks_advanced {
    name = "vaccine-net"
    ipv4_address ="10.0.1.1"
  }

   depends_on = [docker_network.private_network]
}