Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I deploy my docker-compose project using Terraform?

I've looked all over and can't find a coherent resource that describes how to do this straightforwardly. I have a project like so:

./
 |-src/
 |--..
 |--Dockerfile
 |-docker-compose.yaml

A terraform config file like this:

variable "do_token" {}

# Configure the DigitalOcean Provider
provider "digitalocean" {
  token = "${var.do_token}"
}

# Create a web server
resource "digitalocean_droplet" "web" {
  # ...
}

I want to be able to do something like

provider "digitalocean" {
    ip = <my-ip>
    # docker-compose up ?
}

My compose file sets up the application architecture properly. I just want a way to deploy that to a given box somewhere on digital ocean (via ip preferably) and the run docker-compose up. How can I do this?

like image 483
dopatraman Avatar asked Dec 26 '17 21:12

dopatraman


People also ask

How do I initialise terraform via Docker Compose?

Next we’ll go ahead and initialise our Terraform via Docker Compose. Start by authenticating with your AWS IAM account using aws-vault by openening up your Terminal (macOS/Linux) or Command Prompt (Windows), and run the following: (Note: replace account-name with the name of your account you configured in aws-vault).

Is there a docker provisioner for terraform?

The Chef provisioner can also be used with a compose cookbook Terraform has a plain Docker provider but doesn't manage Compose or Swarm definitions out of the box so you would need to define your compose environment piece by piece in volumes, networks, images, containers ).

How do I use Docker with AWS vault and terraform?

Create a docker-compose.yml configuration for Terraform that handles credential management using aws-vault. Create a sample EC2 instance. Run plan, apply and deploy jobs through Docker Compose. Docker Desktop must be installed and working (if you’re using Linux, then ensure Docker and Docker Compose are installed separately)

What is -F deploy/docker-compose?

docker-compose is the name of the docker compose command. -f deploy/docker-compose.yml references the Docker Compose config file we want to run the command on. The command above assumes you’re running the command from the root of your project, but if you are change to the deploy/ directory you can omit this part of the command.


1 Answers

Provisioners

Terraform has provisioners which allow you to copy files and execute scripts on resource creation.

resource "digitalocean_droplet" "web" {
  # ...

  provisioner "file" {
    source      = "compose-app/"
    destination = "/app"
  }

  provisioner "remote-exec" {
    inline = [
      "cd /app",
      "docker-compose up",
    ]
  }
}

The Chef provisioner can also be used with a compose cookbook

Docker

Terraform has a plain Docker provider but doesn't manage Compose or Swarm definitions out of the box so you would need to define your compose environment piece by piece in volumes, networks, images, containers).

provider "docker" {
  host = "tcp://droplet:2375/"
}
resource "docker_image" "myapp" {
  name = "me/myapp:1.0.0"
}
resource "docker_container" "myapp" {
  name = "myapp"
  image = "${docker_image.myapp.latest}"
  ports {
    internal = 1234
    external = 1234
  }
}

Kubernetes

For deploying real world apps With Terraform you are probably better of using the Kubernetes provider that will let you set up a replication controller to run pods that are accessed as services on Docker. This will require running a Kubernetes cluster and writing the Kubernetes definition, Kompose can help converting from Docker Compose.

like image 122
Matt Avatar answered Sep 30 '22 19:09

Matt