DevOps

Run Hashicorp Terraform on Docker

In this tutorial, we are going to explore how to run Terraform on Docker using the official Terraform image.
Captain Salem 2 min read
Run Hashicorp Terraform on Docker

Terraform is an open-source tool used for automating and managing cloud infrastructure through code.

It allows us to define the desired state of their infrastructure using a declarative configuration language called Hashicorp Configuration Language or HCL. This means we can specify in code what resources e need, how they should be configured, and their dependencies.

Terraform will then compare the desired state to the current state of the infrastructure and automatically takes the necessary actions to bring it in line with specifications.

This approach ensures that infrastructure changes are predictable and can be safely applied.

Requirements

To run the commands and steps provided in the this post, ensure you have the following:

  1. Docker engine installed
  2. Docker compose installed
  3. Sufficient permissions to run Docker containers

With the above requirements met, we can proceed.

Docker Pull Terraform

The first step is to download the Docker Terraform image. We can use the docker pull command as shown:

$ docker pull hashicorp/terraform

Running the Container

Once we have pulled the Terraform image, we can create an isolated Terraform container using the docker run command as;

$ docker run -i -t hashicorp/terraform:latest plan

We can define a basic terraform config file as:

terraform {
  required_version = ">= 0.11.1"
}

provider "google" {
  credentials = file(var.gcp_credentials)
  project     = var.gcp_project
  region      = var.gcp_region
}

variable "gcp_credentials" {
  description = "Path to the GCP credentials JSON file"
  type        = string
}

variable "gcp_project" {
  description = "GCP project ID"
  type        = string
}

variable "gcp_region" {
  description = "GCP region, e.g. us-east1"
  default     = "us-east1"
}

variable "gcp_zone" {
  description = "GCP zone, e.g. us-east1-b"
  default     = "us-east1-b"
}

variable "machine_type" {
  description = "GCP machine type"
  default     = "n1-standard-1"
}

variable "instance_name" {
  description = "Name of the GCP instance"
  default     = "demo"
}

variable "image" {
  description = "Image to build the instance from"
  default     = "debian-cloud/debian-9"
}

resource "google_compute_instance" "demo" {
  name         = var.instance_name
  machine_type = var.machine_type
  zone         = var.gcp_zone

  boot_disk {
    initialize_params {
      image = var.image
    }
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }
}

output "external_ip" {
  value = google_compute_instance.demo.network_interface[0].access_config[0].nat_ip
}

The above Terraform configuration provisions a compute instance in Google Cloud Platform.

Conclusion

This explored a basic steps on running Terraform in an isolated environment using Docker containers.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.