Daniel D
Daniel D

Reputation: 3677

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!

Upvotes: 6

Views: 12232

Answers (2)

deobieta
deobieta

Reputation: 278

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.

Upvotes: 11

Innocent Anigbo
Innocent Anigbo

Reputation: 4797

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

Upvotes: 5

Related Questions