gpt4 book ai didi

amazon-web-services - 使用 Terraform 在新创建的虚拟机上创建 Docker 容器

转载 作者:行者123 更新时间:2023-12-03 00:29:28 29 4
gpt4 key购买 nike

我想使用 Terrafom 执行以下任务序列:

  1. 在常见的 IaaS 云中创建虚拟机(我目前使用的是 Google Compute Engine,但 AWS/Azure 等也可以)

  2. 在该虚拟机上安装 Docker

  3. 在该虚拟机上启动 Docker 容器

完成此操作的方式如下:

配置.tf:

module "google_server" {
source="google_module"
name_machine = "server"
}
provider "docker" {
alias="abc"
host="tcp://${module.google_server.ip_address[0]}:2375"
}

resource "docker_container" "container" {
provider = "docker.abc"
...
}

google_module/google_server.tf

provider "google" {
...
}

resource "google_compute_instance" "vm" {
...
}

output "ip_address" {
value = "${google_compute_instance.vm.*.network_interface.0.access_config.0.assigned_nat_ip}"
}

但是我收到以下错误:

module.docker.provider.docker.abc: Error initializing Docker client: invalid endpoint

如果我将 ${module.google_server.ip_address[0]} 替换为随机 IPv4 地址,错误将更改为超时。我能够解决问题的唯一方法是使用 Terraform 通过 SSH 脚本运行 docker。由于这不是很干净,我将不胜感激。

最佳答案

我自己找到了答案。这是open issue在 TerraForm 的 Github 项目中。

常见的解决方法 ( as described here ) 是首先使用 -target 标志运行 TerraForm apply 一次,以便运行多阶段 apply.

在这种情况下,它将是:

$ terraform apply -target=module.google_server
-- works on target first --
$ terraform apply
-- works on rest --

关于amazon-web-services - 使用 Terraform 在新创建的虚拟机上创建 Docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48984775/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com