Sling Academy
Home/DevOps/How to execute Terraform in a Docker container

How to execute Terraform in a Docker container

Last updated: February 03, 2024

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.

Next Article: Terraform CLI commands: The complete cheat sheet

Previous Article: Methods to Install Terraform on Ubuntu

Series: Terraform Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide