Using Terraform with docker-compose and nginx-proxy

Daniel D picture Daniel D · Jul 3, 2017 · Viewed 9.6k times · Source

Has anyone tried using all these tools together?

I'm currently using nginx-proxy and docker-compose for a four-container solution.

I'm now trying to make deployment better/faster/cheaper and think terraform might be the piece I'm now looking for.

My question is - does terraform work with docker-compose? Or is there too much overlap between them?

Thanks for any advice!

Answer

deobieta picture deobieta · Jul 4, 2017

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'

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}"
}

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.