Simplifying Docker Deployment with Terraform: A Comprehensive Guide

Simplifying Docker Deployment with Terraform: A Comprehensive Guide

Day 61: Docker Provider in Terraform

ยท

3 min read

Introduction

In the realm of modern infrastructure management, Infrastructure as Code (IaC) tools like Terraform streamline the deployment and management of resources. Terraform, renowned for its declarative configuration language, empowers users to define and provision infrastructure effortlessly. In this comprehensive guide, we'll explore how Terraform simplifies the deployment of Dockerized applications using NGINX as an example.


Task-01: Setting Up Terraform Script with Blocks and Resources

1.1. Install Terraform

Ensure 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 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 configures Terraform with the necessary Docker provider.


Task-02: Defining 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, include 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_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. Verification

Verify that the NGINX Docker container is running:

docker ps


Conclusion

Congratulations! You've successfully created a Terraform script to deploy the NGINX Docker image and run a container. This automation streamlines the deployment and management of Dockerized applications, highlighting the power and flexibility of Terraform. Harness Terraform's capabilities to simplify your infrastructure management tasks and unlock new levels of efficiency in your workflow.

Happy Learning ๐Ÿš€

Follow me on LinkedIn.

ย