#90DaysOfDevOps Challenge - Day 62 - Terraform and Docker

#90DaysOfDevOps Challenge - Day 62 - Terraform and Docker

Welcome to Day 62 of the #90DaysOfDevOps Challenge. Today, we dive into the powerful combination of Terraform and Docker. We'll explore blocks and resources in Terraform, and learn how to integrate Docker into our infrastructure automation.

Blocks and Resources in Terraform

Terraform Block

The terraform block in Terraform is used to configure the global settings and required providers for the Terraform configuration. It allows you to define the version constraints for the providers you're using in your infrastructure. In our case, we will define the Docker provider and specify its version to ensure compatibility with our Terraform code.

Example:

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

Provider Block

The provider block is used to configure the specific provider that Terraform will use to manage the resources. It specifies the details of the provider, such as its name, source, and any required authentication or configuration settings. In our case, we will configure the Docker provider using the kreuzwerker/docker registry as the source.

Example:

provider "docker" {
  host     = "tcp://<DOCKER_HOST>:<DOCKER_PORT>"
  registry = "registry.terraform.io/kreuzwerker/docker"
}

Resource

A resource block represents a single infrastructure component that you want to manage with Terraform. It defines the desired state of that resource and specifies the configuration parameters required to create or manage it. In the context of our Terraform and Docker integration, we will create resource blocks for Docker images and containers. The resource block for the Docker image will define the image name and other related attributes, while the resource block for the Docker container will define the container's image, name, and any additional configuration like port mappings.

Example - Docker Image:

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

Example - Docker Container:

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 80
  }
}

By using blocks and resources in Terraform, you can define and manage your infrastructure components in a declarative manner. This allows for consistent and reproducible deployments, making it easier to maintain and scale your infrastructure as your needs evolve.

Task 1: Creating Provider block and resource

Terraform block

  1. Start by creating a new Terraform script docker_terraform.tf

  2. Define the terraform block and specify the required providers. Use kreuzwerker/docker as the Docker provider.

     terraform {
       required_providers {
         docker = {
           source  = "kreuzwerker/docker"
           version = "~> 2.21.0"
         }
       }
     }
    
  3. Save the file with a .tf extension.

Provider block

  1. Add the below provider block to the docker_terraform.tf file

     provider "docker" {}
    

Task 2: Creating Resource Blocks for Nginx Docker Image and Container

  1. Add the following code to create a resource block for the nginx Docker image:

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

  2. Add the following code to create a resource block for running a Docker container for nginx:

     resource "docker_container" "nginx" {
       image = docker_image.nginx.latest
       name  = "tutorial"
       ports {
         internal = 80
         external = 80
       }
     }
    

    Note: If Docker is not installed on your system, run the following commands to install it:

     sudo apt-get install docker.io
     sudo usermod -aG docker $USER
     sudo reboot
     docker ps
    
  3. Now, we have our terraform file ready. Let's execute it.

  4. Initialize the Terraform configuration:

       terraform init
    

  5. Review the execution plan of changes:

       terraform plan
    

  6. Apply the changes to the infrastructure:

       terraform apply
    

  7. Once the Docker container for the Nginx image is created, access the public URL to view the Nginx default webpage.

  8. We can use the terraform destroy command to delete the infrastructure created

Congratulations! You've learned how to use Terraform and Docker together. With Terraform, you can define and manage Docker resources, enabling you to build scalable and reproducible infrastructure. Stay tuned for day 63 of the #90daysofdevops challenge, where we'll dive into Terraform variables.

Did you find this article valuable?

Support Esteban Moreno by becoming a sponsor. Any amount is appreciated!