Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Terraform with docker-compose and nginx-proxy

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!

like image 210
Daniel D Avatar asked Jul 03 '17 15:07

Daniel D


2 Answers

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.

like image 61
deobieta Avatar answered Oct 19 '22 19:10

deobieta


You can run single or multiple docker container in Terraform using the docker provider.

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

Sample nginx terraform config

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
  }
}
like image 5
Innocent Anigbo Avatar answered Oct 19 '22 18:10

Innocent Anigbo