8  Week 6 (06-Nov-2025)


8.1 Terraform Introductory Exercises

8.1.1 Terraform with Local Docker - Code Example

Title: Terraform for Local Docker Containers

  • This code uses Terraform to define and run a Docker container locally. It requires the Docker provider, pulls an image, and runs a container with specific configuration.

  • Create a directory called example1.

  • Create a file inside that directory called nginx.tf

  • Include the following contents in this file.

Example Code:


# Install the Docker provider (if needed)
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}

# Define the Docker provider
provider "docker" {
  host = "unix:///var/run/docker.sock"
  # Use this below configuration instead if Docker is running on Windows
  # host = "npipe:////./pipe/docker_engine"
}

# Pull the latest Nginx image
resource "docker_image" "nginx_image" {
  name = "nginx:latest"
}

# Run a Docker container using the pulled Nginx image
resource "docker_container" "nginx_container" {
  image = docker_image.nginx_image.image_id
  name  = "nginx_server"
  ports {
    internal = 80
    external = 8080
  }
}

Explanation:

  1. Provider: Connects to Docker using the local Docker socket.

  2. Image Resource: Downloads the Nginx Docker image.

  3. Container Resource: Creates a container from the Nginx image, exposing it on port 8080 locally.

  • In the example1 directory run the follwing commands one by one and check the results.

Commands to Run:

  • terraform init: Initializes the Docker provider.

  • terraform plan: To validate the configuration without really executing it.

  • terraform apply: Provisions the Nginx container.

8.1.2 Terraform with Local Docker using variables

  • Based on the class slides, convert the above exercise to use a variables file and apply it to this scenario.

  • The parameter variables are the name of the container and the external port.

Example Code:

File: main.tf

# main.tf
# Install the Docker provider (if needed)
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}

# Define the Docker provider
provider "docker" {
  host = "unix:///var/run/docker.sock"
  # Use this below configuration instead if Docker is running on Windows
  # host = "npipe:////./pipe/docker_engine"
}

# Pull the latest Nginx image
resource "docker_image" "nginx_image" {
  name = "nginx:latest"
}

# Run a Docker container using the pulled Nginx image
resource "docker_container" "nginx_container" {
  image = docker_image.nginx_image.image_id
  name  = var.container_name
  ports {
    internal = 80
    external = var.external_port
  }
}
  • Create a file inside that directory called variables.tf

  • Include the following contents in this file.

File: variables.tf

# variables.tf
variable "container_name" {
  description = "Name of the Docker container"
  type        = string
}

variable "external_port" {
  description = "External port for the Docker container"
  type        = number
}
  • Create a file inside that directory called terraform.tfvars

  • Include the following contents in this file.

File: terraform.tfvars

# terraform.tfvars
container_name = "my_nginx_container"
external_port  = 8080

Commands to Run:

  • terraform init: Initializes the Docker provider.

  • terraform validate: It verifies that the configuration files (.tf and/or .tf.json) are syntactically correct.

  • terraform plan: To validate the configuration without really executing it.

  • terraform apply: Provisions the Nginx container.

8.1.3 Deploying MySQL to Local Docker with Terraform

  • Using the same approach, deploy a MySQL container to docker.

  • The parameter variables are the name of the container, the external port, the database name and the root password .

  • You may search Google and/or ChatGPT to implement this exercise.

  • This code uses Terraform to define and run a MySQL Docker container locally. It requires the Docker provider, pulls an image, and runs a container with specific configuration.

  • Create a directory called example2.

  • Create a file inside that directory called main.tf

  • Include the following contents in this file.

File: main.tf

# main.tf
# Install the Docker provider (if needed)
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}

# Define the Docker provider
provider "docker" {
  host = "unix:///var/run/docker.sock"
  # Use this below configuration instead if Docker is running on Windows
  # host = "npipe:////./pipe/docker_engine"
}

resource "docker_image" "mysql" {
  name = "mysql:latest"
}

resource "docker_container" "mysql" {
  image = docker_image.mysql.image_id
  name  = var.container_name

  ports {
    internal = 3306
    external = var.external_port
  }

  env = [
    "MYSQL_DATABASE=${var.database_name}",
    "MYSQL_ROOT_PASSWORD=${var.root_password}"
  ]
}
  • Create a file inside that directory called variables.tf

  • Include the following contents in this file.

File: variables.tf

variable "container_name" {
  description = "Name of the Docker container"
  type        = string
}

variable "external_port" {
  description = "External port for the MySQL container"
  type        = number
}

variable "database_name" {
  description = "Name of the MySQL database to be created"
  type        = string
}

variable "root_password" {
  description = "Root password for the MySQL instance"
  type        = string
  sensitive   = true
}
  • Create a file inside that directory called terraform.tfvars

  • Include the following contents in this file.

File: terraform.tfvars

# terraform.tfvars
container_name = "mysql_container"
external_port  = 3306
database_name  = "my_database"
root_password  = "strongpassword123"

Commands to Run:

  • terraform init: Initializes the Docker provider.

  • terraform validate: It verifies that the configuration files (.tf and/or .tf.json) are syntactically correct.

  • terraform plan: To validate the configuration without really executing it.

  • terraform apply: Provisions the Nginx container.