dockerdocker-composeterraform

docker-compose.yml to Terraform.tf


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  

Solution

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