How to execute Terraform in a Docker container

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

Terraform by HashiCorp is an Infrastructure as Code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently. It supports a variety of service providers such as Amazon Web Services, Microsoft Azure, Google Cloud Platform, and more. Docker, on the other hand, is a platform for developing, shipping, and running applications in containers. Combining Terraform with Docker can streamline the process of infrastructure management, ensuring consistency across development and production environments. In this tutorial, we will learn how to execute Terraform in a Docker container, walking through multiple examples from basic usages to more advanced scenarios.

Prerequisites

  • Docker installed on your system.
  • Terraform knowledge (basic understanding).

Getting Started

To begin, we need to pull the official Terraform Docker image from Docker Hub. This image contains all the necessary tools to run Terraform.

docker pull hashicorp/terraform:light

Verify the image was successfully pulled:

docker images

Basic Terraform Execution

First, navigate to a directory where your Terraform configuration files are located. Then, you can run Terraform commands using the pulled Docker image as follows:

docker run --rm -v ${PWD}:/workspace -w /workspace hashicorp/terraform:light init
docker run --rm -v ${PWD}:/workspace -w /workspace hashicorp/terraform:light apply

This will initialize your Terraform workspace and apply your configuration, respectively. The --rm option ensures that the container is removed after execution, and the use of volumes (-v) shares your current directory with the container.

APPlying Configuration with Custom Variables

Often, Terraform configurations require variables. You can pass these to your Docker Terraform container as follows:

docker run --rm -v ${PWD}:/workspace -w /workspace -e TF_VAR_my_variable=value hashicorp/terraform:light apply

This method allows you to dynamically inject variables into your Terraform execution environment.

Advanced: Terraform with Docker as a Backend

It’s possible to use Docker not just to run Terraform but also to maintain state files. Implementing this requires more setup and is recommended for advanced users.

With Terraform, the state can be managed in various backends for scalability and collaboration purposes. Here’s how you can achieve storing your state in a Docker volume.

docker volume create terraform_state

After creating the volume, you can mount it inside your Terraform Docker container:

docker run --rm -v terraform_state:/terraform_state -v ${PWD}:/workspace -w /workspace hashicorp/terraform:light init

This setup ensures that your Terraform state is persistently stored, making it easier for team collaborations and state management.

Using Terraform in a CI/CD Pipeline

Integrating Terraform within a Continuous Integration and Continuous Delivery (CI/CD) pipeline through Docker can automate and streamline the deployment process. Here are the steps to utilize Terraform in a Docker-based CI/CD pipeline:

## In your CI/CD pipeline configuration file

# Step 1: Pull the Terraform Docker image
- docker pull hashicorp/terraform:light

# Step 2: Initialize the Terraform workspace
- docker run --rm -v ${PWD}:/workspace -w /workspace hashicorp/terraform:light init

# Step 3: Plan and apply the configuration
- docker run --rm -v ${PWD}:/workspace -w /workspace hashicorp/terraform:light plan
- docker run --rm -v ${PWD}:/workspace -w /workspace hashicorp/terraform:light apply

Integrating Terraform in this way leverages Docker’s portability and scalability, ensuring your infrastructure deployment is consistent across all environments.

Conclusion

Running Terraform in a Docker container simplifies the management of infrastructure deployments, ensuring consistency and scalability. Whether for individual use or within a team, following this guide helps integrate Terraform into your workflow, allowing for efficient infrastructure management and deployment. Remember to adapt the examples shown to fit your specific use cases and requirements.