Skip to content

Terraform

Setting Up Terraform on Docker Compose

Terraform is a Infrastructure as Code tool that allows for the safe provisioning and management of infrastructure in any cloud environment where there is a Provider available.

A Provider is a supported mechanism, typically a logical abstrastion of an upstream API that allows for the creation of resources using API Interactions and Exposing Resources.

Why Choose Terraform

Terraform is one of many solutions that offer a similar management style but is also one of the main industry standard solutions, and due to being one of the main industry standard solutions there are a lot of external companies that support Terraform and have a Provider already available.

For example AWS, Azure, GCP, Kubernetes, Helm, Nomad, Oracle Cloud Platform, Hashicorp Vault, VMware vSphere to name a few.

Docker Compose Example

Below is an example Terraform configuration using Docker Compose.

As shown, the configuration is fairly simple and does not require a lot of configuration to allow Terraform to run.

Note: The Terraform docker container does not require to be always running. The container itself only runs when executing commands and then stops.

services:
terraform:
image: hashicorp/terraform:1.9.6
volumes:
- ./config:/infra
working_dir: /infra
environment:
- AUTHENTIK_URL=https://authentik.<domain>
- AUTHENTIK_TOKEN=

Start Terraform Container

To start the Terraform docker compose container, run the following command where the docker-compose.yaml file has been created / saved.

docker compose up -d

Terraform Commands using Docker Compose

Executing Terraform commands such as terraform init, terraform validate. terraform plan,terraform apply, terraform plan -target=, terraform apply -target=, terraform fmt, terraform state, terraform show use the following command, replacing with the relevant Terraform command that needs to be executed. Make sure that the docker-compose.yaml file is in the current directory otherwise the command will possibly error.

docker compose -f docker-compose.yaml run --rm <terraform command>

An example folder structure based on the above docker-compose.yaml file is below:

.
├── config
│   ├── applications.tf
│   ├── certificate.tf
│   ├── flow.tf
│   ├── group.tf
│   ├── main.tf
│   ├── provider.tf
│   ├── scope.tf
│   ├── terraform.tfstate
│   └── terraform.tfstate.backup
└── docker-compose.yaml

As shown above, all configuration including terraform.tfstate and terraform.tfstate.backup are within the config folder. The two terraform* files are important as these are what terraform itself uses to reference the resources it manages. Removing the terraform* files will result in the resources needing to either be redeployed or reimported into terraform.

Key Components of Docker Compose Configuration Explained

  • Image - Specifies the Terraform server image and tag
  • Volumes - For where the configuration files to be passed into the container, are going to be located
  • Working_dir - Sets the working directory for the container, useful for Terraform as it sets the working directory to where all the configuration files are being kept.
  • Environment - Configures environment variables referenced by the container