如何使用Terraform部署docker-compose项目? [英] 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" {
# ...
}
我希望能够
provider "digitalocean" {
ip = <my-ip>
# docker-compose up ?
}
我的撰写文件正确设置了应用程序体系结构。我只想要一种方法(将其最好地通过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?
推荐答案
资源调配者
Terraform有<一个href = https://www.terraform.io/docs/provisioners/index.html rel = noreferrer> provisioners ,它使您可以复制文件并在创建资源时执行脚本。
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",
]
}
}
Terraform具有简单的 Docker提供程序,但无法直接管理Compose或Swarm定义,因此您需要在 vo lumes ,网络,图像,容器)。
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
用于部署实际应用在Terraform上,最好使用 Kubernetes提供程序设置复制控制器以运行 pod 以服务es 。这将需要运行Kubernetes集群并编写Kubernetes定义, Kompose 可以帮助从Docker Compose转换。
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.
这篇关于如何使用Terraform部署docker-compose项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!