Skip to content

Instantly share code, notes, and snippets.

@potikanond
Last active February 10, 2026 03:46
Show Gist options
  • Select an option

  • Save potikanond/29bc46ff024ec47b66f4cd3465714c67 to your computer and use it in GitHub Desktop.

Select an option

Save potikanond/29bc46ff024ec47b66f4cd3465714c67 to your computer and use it in GitHub Desktop.
Terraform Tutorial - Docker Provider

Terraform Tutorial - Docker Provider

To use the Terraform Docker provider, you define the provider configuration and then use its resources (like docker_image and docker_container) to manage Docker components. The process involves writing the configuration file, initializing the project, and applying the changes.

Prerequisites

Before starting, ensure you have the following installed:

  • Terraform CLI
  • Docker CLI and Daemon (e.g., Docker Desktop, configured with a default profile)

Step-by-Step Tutorial

1. Set Up Your Project Directory

Create a new directory for your Terraform project and navigate into it:

mkdir terraform-docker
cd terraform-docker

Create a file named main.tf in this directory, where all the configuration will go.

2. Define the Docker Provider and Resources

Open main.tf in a text editor and add the following configuration. This configuration specifies the required provider and defines resources to pull an NGINX image and run it in a container, exposing port 8080 externally.

# main.tf

# Define the required providers and their versions
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 3.0"
    }
  }
}

# Configure the Docker provider (uses local Docker socket by default)
provider "docker" {}

# Define a data source to pull the NGINX image
data "docker_image" "nginx" {
  name = "nginx:latest"
}

# Define a resource to create and manage a Docker container
resource "docker_container" "nginx_container" {
  name  = "nginx_example"
  image = data.docker_image.nginx.image_id
  ports {
    internal = 80
    external = 8080
  }
}
  • Provider Block: Tells Terraform to use the kreuzwerker/docker provider.
  • Data Source docker_image: Instructs Terraform to fetch the specified Docker image (nginx:latest). It ensures the image is available before attempting to create a container.
  • Resource docker_container: Defines the container to run, linking it to the image data source and mapping internal port 80 to external port 8080.

3. Initialize the Terraform Project

Run terraform init in your terminal. This command downloads the Docker provider plugin.

terraform init

4. Plan and Apply the Configuration

Review the changes Terraform plans to make using terraform plan.

terraform plan

If the plan looks correct, apply the configuration to create the Docker container.

terraform apply

Type yes when prompted to confirm the action.

5. Verify the Deployment

After Terraform finishes, the NGINX container will be running. You can verify this by visiting http://localhost:8080 in your web browser or by running the Docker CLI command:

docker ps -a

6. Destroy the Infrastructure

To remove the created container and the image, run terraform destroy. This cleans up all resources managed by your Terraform configuration.

terraform destroy

Terraform Variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment