Dockerizing Terraform

Terraform is an amazing IaC tool. You can use Docker to run Terraform in a containerized environment. I'll share what I leared about Dockerizing Terraform.


Terraform3001
Docker1001

Why run Terraform in a Container?

I like to keep my development environment as clean as possible. Containers are a great way to do that. You can run Terraform in a containerized environment to keep your host machine clean. You can also use containers to run Terraform in a CI/CD pipeline.

Prerequisites

  • Docker
  • A working directory with Terraform files
  • An environment which supports Terraform (e.g. Azure, AWS, GCP)
  • ARM_ACCESS_KEY environment variable (for Azure)

Docker Image

We will be using the official HashiCorp Terraform Docker image. You can find the image on Docker Hub at https://hub.docker.com/r/hashicorp/terraform.

Running Terraform in a Container

You can run Terraform in a container using the following commands. The commands mount the current working directory to the container using -v and -w parameters, and set the ARM_ACCESS_KEY environment variable using the -e parameter. After running the commands, the containers are removed using the -rm parameter.

terraform init

docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest init

terraform plan -out=main.tfplan

docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest plan -out=main.tfplan

terraform apply main.tfplan

docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest apply main.tfplan

terraform destroy

docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest destroy

Using Aliases

As you probably noticed, the commands to run Terraform in a container are quite long and cumbersome. I created aliases in my .zshrc file to make it easier to run Terraform in a container. Here are the aliases I use:

# Dockerized Terraform
alias dtfinit="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest init"
alias dtfinitrc="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest init -reconfigure"
alias dtfplan="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest plan -out main.tfplan"
alias dtfapply="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest apply main.tfplan"
alias dtfdestroy="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest destroy"
alias dtfrefresh="docker run -i -t --rm -v "$(pwd):/workspace" -w /workspace -e ARM_ACCESS_KEY=$ARM_ACCESS_KEY hashicorp/terraform:latest refresh"

Conclusion

Running Terraform in a container is a great way to keep your development environment clean. It is a good thing that Hashicorp provides an official Terraform Docker image. I try to use containers as much as possible in my workflow to learn more about containers and to get ready to move on to CI/CD pipelines and kubernetes soon. I hope you found this article helpful. Let me know if you have any questions or suggestions.