kreuzwerker / terraform-provider-docker

Terraform Docker provider
Mozilla Public License 2.0
609 stars 187 forks source link

Using a file for storing environment variables #629

Open nmcc1212 opened 3 months ago

nmcc1212 commented 3 months ago

Community Note

Terraform (and docker Provider) Version

TF v1.8.5, Provider version "~> 3.0.1"

Affected Resource(s)

Terraform Configuration Files

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0.1"
    }
  }
}

provider "docker" {}

# Define the MongoDB image resource
resource "docker_image" "mongo" {
  name         = "mongo"
  keep_locally = false
}

# Define the icds-frontend image resource
resource "docker_image" "icds_frontend" {
  name         = "ghcr.io/nmcc1212/icds-frontend:v3"
  keep_locally = false
}

# Define the volume for MongoDB data
resource "docker_volume" "mongo_data" {
  name = "mongo-data"
}

# Define the db container resource
resource "docker_container" "db" {
  image = docker_image.mongo.image_id
  name  = "db"
  hostname = "db"

  ports {
    internal = 27017
    external = 27017
  }

  volumes {
    volume_name    = docker_volume.mongo_data.name
    container_path = "/data/db"
  }

  volumes {
    host_path      = "${path.module}/mongo/init.sh"
    container_path = "/docker-entrypoint-initdb.d/init.sh"
    read_only      = true
  }

  volumes {
    host_path      = "${path.module}/mongo/socialAPI.posts.json"
    container_path = "/docker-entrypoint-initdb.d/socialAPI.posts.json"
    read_only      = true
  }
}

# Define the icds-frontend container resource
resource "docker_container" "icds_frontend" {
  image = docker_image.icds_frontend.image_id
  name  = "icds-frontend"

  ports {
    internal = 3000
    external = 3000
  }

  env = file(".tf.env")
}

and the .tf.env file (obvious redactions)

[
  "AUTH0_SECRET=x",
  "AUTH0_BASE_URL=http://localhost:3000",
  "AUTH0_ISSUER_BASE_URL=x",
  "AUTH0_CLIENT_ID=x",
  "AUTH0_CLIENT_SECRET=x",
  "API_URL = x",
  "MONGO_URI = mongodb://db:27017/socialAPI"
]

Debug Output

Panic Output

Expected Behaviour

Using the environment variables from the file

Actual Behaviour

│ 68: env = file(".tf.env") │ │ Inappropriate value for attribute "env": set of string required.

Steps to Reproduce

Use provided tf code and create a .tf.env file with environment variables

  1. terraform apply

Important Factoids

References