Reputation: 13
I am admittedly play around. But, only way to learn is to start at the bottom. I am trying to figure out how to get this most basic of docker-compose yaml files into an equivalent terraform.tf file. Yes, I know, in a production environment with 1000 developers this would not be how it's done, but I'm still learning basics.
my docker-compse.yml looks like this:
services:
myapp:
container_name: myapp
image: myapphub/myapp
network_mode: host
cap_add:
- NET_ADMIN
devices:
- /dev/net/tun
environment:
MYAPP_ENROLMENT_KEY: ?? how do i get this from variables.tf ??
volumes:
- myapp-config:/etc/myapp/profiles
- myapp-logs:/var/log/myapp
restart: unless-stopped
watchtower:
image: containrrr/watchtower
volumes:
- /var/run/docker.sock:/var/run/docker.sock
restart: unless-stopped
volumes:
myapp-config:
myapp-logs:
main.tf looks like this:
resource "docker_container" "myapp" {
name = "myapp"
image = "myapphub/myapp"
}
And variables.tf looks like this:
resource "docker_volume" "my_volume" {
name = "my-volume"
variable "container_name" {
description = "Value of the name for the Docker container"
type = string
default = "ExampleNginxContainer"
}
So how would I get all the additional directives that are contained in the docker-compose.yml into the terraform.tf? Or is there where I am trying to use a fork as a spoon?
network_mode: host
cap_add:
- NET_ADMIN
devices:
- /dev/net/tun
environment:
MYAPP_ENROLMENT_KEY: ?? how do i get this from variables.tf ??
volumes:
- myapp-config:/etc/myapp/profiles
- myapp-logs:/var/log/myapp
restart: unless-stopped
Upvotes: -1
Views: 50
Reputation: 29
I think this is what you're looking for - I've included comments just to clarify what each part of the Terraform code does:
variable "myapp_enrolment_key" {
description = "The enrolment key for myapp"
type = string
}
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = ">= 2.13.0" # Adjust the version constraint as
needed
}
}
}
# Pull images
resource "docker_image" "myapp" {
name = "myapphub/myapp"
}
resource "docker_image" "watchtower" {
name = "containrrr/watchtower"
}
# Create Docker volumes
resource "docker_volume" "myapp_config" {
name = "myapp-config"
}
resource "docker_volume" "myapp_logs" {
name = "myapp-logs"
}
# Create the "myapp" container
resource "docker_container" "myapp" {
name = "myapp"
image = docker_image.myapp.latest
network_mode = "host"
restart = "unless-stopped"
# Pass the environment variable using the value from variables.tf
env = [
"MYAPP_ENROLMENT_KEY=${var.myapp_enrolment_key}",
]
# Add the NET_ADMIN capability (equivalent to cap_add)
capabilities = ["NET_ADMIN"]
# Map the device (/dev/net/tun)
device {
host_path = "/dev/net/tun"
container_path = "/dev/net/tun"
}
# Mount volumes
mount {
type = "volume"
source = docker_volume.myapp_config.name
target = "/etc/myapp/profiles"
}
mount {
type = "volume"
source = docker_volume.myapp_logs.name
target = "/var/log/myapp"
}
}
# Create the "watchtower" container
resource "docker_container" "watchtower" {
name = "watchtower"
image = docker_image.watchtower.latest
restart = "unless-stopped"
mount {
type = "bind"
source = "/var/run/docker.sock"
target = "/var/run/docker.sock"
}
}
Upvotes: 0
Reputation: 12190
I have not used this provider, but looking at the doc everything in your docker-compose file has a corresponding attribute in the terraform config. I have knocked this up in about 5 minutes you might need to adjust it a little but should give you enough to get started.
Unless I have miss understood your issue.
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "3.0.2"
}
}
}
variable "enrolment_key" {
description = "The enrolment key for the app"
type = string
}
resource "docker_container" "myapp" {
name = "myapp"
image = "myapphub/myapp"
network_mode = "host"
capabilities {
add = ["NET_ADMIN"]
}
devices {
host_path = "/dev/net/tun"
}
env = [
"MYAPP_ENROLMENT_KEY: ${var.enrolment_key}",
]
volumes {
volume_name = docker_volume.myapp_config.name
container_path = "/etc/myapp/profiles"
}
volumes {
volume_name = docker_volume.myapp_logs.name
container_path = "/var/log/myapp"
}
restart = "unless-stopped"
}
resource "docker_container" "watchtower" {
image = "containrrr/watchtower"
name = "watchtower"
volumes {
host_path = "/var/run/docker.sock"
container_path = "/var/run/docker.sock"
}
restart = "unless-stopped"
}
resource "docker_volume" "myapp_config" {
name = "myapp-config"
}
resource "docker_volume" "myapp_logs" {
name = "myapp-logs"
}
Upvotes: 1