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

后端 未结 1 669
[愿得一人]
[愿得一人] 2021-02-14 06:22

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/
 |--..
 |--Docker         


        
1条回答
  •  广开言路
    2021-02-14 07:06

    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.

    0 讨论(0)
提交回复
热议问题