【问题标题】:Using Terraform with docker-compose and nginx-proxy将 Terraform 与 docker-compose 和 nginx-proxy 一起使用
【发布时间】:2017-07-03 15:58:47
【问题描述】:

有没有人尝试过同时使用所有这些工具?

我目前正在使用 nginx-proxy 和 docker-compose 作为四容器解决方案。

我现在正在尝试使部署更好/更快/更便宜,并认为 terraform 可能是我现在正在寻找的部分。

我的问题是 - terraform 可以与 docker-compose 一起使用吗?还是它们之间有太多重叠?

感谢您的建议!

【问题讨论】:

  • terraform 是基础设施即代码,不是配置工具,我认为你用错了工具,应该由 ansible、puppet、chef、salt 或其他自动化工具完成

标签: docker-compose terraform jwilder-nginx-proxy


【解决方案1】:

您可以按照已经建议的方式使用 Terraform 提供程序,但如果您出于任何原因想要坚持使用 docker-compose,您还可以创建 docker-compose 文件并使用用户数据运行必要的命令。看看template_filetemplate_cloudinit_config

例子

nginx.tpl

#cloud-config
write_files:
 - content: |
        version: '2'
        services:
            nginx:
              image: nginx:latest
   path: /opt/docker-compose.yml
runcmd:
 - 'docker-compose -f /opt/docker-compose.yml up -d'

nginx.tf

data "template_file" "nginx" {
    template = "${file("nginx.tpl")}"
}

resource "aws_instance" "nginx" {
    instance_type = "t2.micro"
    ami = "ami-xxxxxxxx"

    user_data = "${data.template_file.nginx.rendered}"
}

我使用 AWS,但这应该适用于任何支持用户数据的提供商和带有 cloud-init 的盒子。这种方法也适用于自动缩放。

【讨论】:

    【解决方案2】:

    您可以使用 docker 提供程序在 Terraform 中运行单个或多个 docker 容器。

    https://www.terraform.io/docs/providers/docker/index.html

    示例 nginx terraform 配置

    provider "docker" {
      host = "tcp://ec2-xxxxxxx.compute.amazonaws.com:2375/"
    }
    resource "docker_image" "nginx" {
      name = "nginx:1.11-alpine"
    }
    resource "docker_container" "nginx-server" {
      name = "nginx-server"
      image = "${docker_image.nginx.latest}"
      ports {
        internal = 80
        external = 80
      }
      volumes {
        container_path  = "/usr/share/nginx/html"
        host_path = "/home/scrapbook/tutorial/www"
        read_only = true
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-07
      • 2019-12-25
      • 1970-01-01
      • 1970-01-01
      • 2020-07-23
      • 2021-02-15
      • 2019-12-04
      • 2016-10-23
      • 2021-01-28
      相关资源
      最近更新 更多