Day 62: Docker Provider in Terraform

Day 62: Docker Provider in Terraform

Introduction

Modern infrastructure management involves the use of Infrastructure as Code (IaC) tools, providing a streamlined and automated approach to deploying and managing resources. Terraform, one such tool, enables users to define and provision infrastructure using a declarative configuration language. In this guide, we'll walk through the process of using Terraform to pull an NGINX Docker image and run a container, showcasing how Terraform simplifies the deployment of Dockerized applications.

Task-01: Create a Terraform Script with Blocks and Resources

1.1. Install Terraform

Begin by ensuring that Terraform is installed on your machine. You can download the latest version from the official Terraform website.

1.2. Create a Directory

Set up a new directory for your Terraform project.

mkdir nginx-terraform
cd nginx-terraform

1.3. Create Terraform Script

Create a new file named main.tf within the directory and add the following content:

# main.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.21.0"
    }
  }
}

provider "docker" {}

This script sets up the Terraform configuration with the necessary Docker provider.

Task-02: Create Resource Blocks for NGINX Docker Image and Container

2.1. NGINX Docker Image Resource Block

Add the following code to main.tf to define the Docker image resource block:

# main.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.21.0"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

This resource block specifies the NGINX Docker image with the latest tag.

2.2. NGINX Docker Container Resource Block

Now, add the resource block for running the NGINX Docker container:

# main.tf

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.21.0"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"

  ports {
    internal = 80
    external = 80
  }
}

This resource block defines a Docker container based on the NGINX image, named "tutorial," with port mapping from internal (container) port 80 to external (host) port 80.

Execution

3.1. Initialize Terraform

Run the following command to initialize Terraform within your project directory:

terraform init

3.2. Apply Changes

Apply the changes to create the NGINX Docker image and container:

terraform apply

Terraform will prompt for confirmation; type yes and press Enter.

3.3. Verify

Verify that the NGINX Docker container is running by accessing it through a web browser or using the following command:

docker ps

You should see a running container named "tutorial" with NGINX.

Conclusion

Congratulations! You have successfully created a Terraform script to pull the NGINX Docker image and run a container. This automation simplifies the deployment and management of Dockerized applications, showcasing the power and flexibility of Terraform.

Follow me on LinkedIn.

Checkout my GitHub profile.