将 Terraform 与 docker-compose 和 nginx-proxy 一起使用

Dan*_*l D 4 docker-compose terraform jwilder-nginx-proxy

有没有人试过一起使用所有这些工具?

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

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

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

感谢您的任何建议!

小智 11

You can use Terraform provider as already suggested but if you want to stick to docker-compose for any reason you can also create your docker-compose file and run the necessary commands with user-data. Take a look to template_file and template_cloudinit_config

Example

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'
Run Code Online (Sandbox Code Playgroud)

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}"
}
Run Code Online (Sandbox Code Playgroud)

I use AWS but this should work with any provider supporting user-data and a box with cloud-init. Also this approach is suitable for autoscaling.


Inn*_*gbo 5

您可以使用 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
  }
}
Run Code Online (Sandbox Code Playgroud)