Day 62 - Terraform and Docker πŸ”₯

Β·

4 min read

Day 62 - Terraform and Docker πŸ”₯

As we continue our journey into Infrastructure as Code (IAAC), today's focus is on Terraform and Docker, two powerful tools for modern cloud and DevOps workflows. Terraform is used for provisioning infrastructure, while Docker is essential for containerization. Combining them allows us to automate the creation, management, and scaling of containerized applications. Let's dive into how to manage Docker containers using Terraform with practical examples!

Blocks and Resources in Terraform

Terraform configurations are built using blocks, which define the components of your infrastructure. These blocks could be for providers, resources, variables, or outputs.

Syntax of a Block in Terraform:

<block_type> <resource_type> <resource_name> {
    arguments
}

What is a Block in Terraform?

Blocks define different aspects of the configuration such as the provider, resource, variable, or output. Each block has a specific purpose and contains configuration settings relevant to that block type.

What are Arguments in Terraform?

Within each block, arguments are used to set configuration values. The syntax for an argument is key = value, where the key is the configuration setting name and the value is the assigned value for that setting.

What are Resources in Terraform?

A resource is a specific type of block used to define and manage components of your infrastructure. For example, a resource might be a Docker container or an AWS EC2 instance. Terraform uses resource blocks to specify the desired state of these components and works to create, update, or destroy them as needed.

Task 1: Create a Terraform Script with Blocks and Resources

Let's start by creating a Terraform script to manage Docker containers.

Step 1: Creating the Terraform Configuration File

First, we need to create a main.tf file and add the required Docker provider. Here's the basic setup:

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

Step 2: Provider Block

Next, we define the provider block, which configures Terraform to interact with Docker.

provider "docker" {}
  • The docker provider allows Terraform to interact with Docker's API to create and manage containers, images, and other Docker components.

Task 2: Create Resource Blocks for a Nginx Docker Image and Container

Now, let’s create a resource block to define a Docker image and container.

Step 1: Define an Nginx Docker Image

We'll use the docker_image resource to pull the latest Nginx image from Docker Hub:

resource "docker_image" "my_nginx" {
  name         = "nginx:latest"
  keep_locally = false
}
  • docker_image: This defines the type of resource.

  • my_nginx: The logical name used to reference this resource.

  • name: The Docker image name, in this case, nginx:latest.

  • keep_locally: This argument specifies whether to keep the image locally after use.

Step 2: Create a Docker Container for Nginx

Next, we create a Docker container for Nginx and expose port 80:

resource "docker_container" "my_nginx_container" {
  image = docker_image.my_nginx.name
  name  = "nginx-container"
  ports {
    internal = 80
    external = 80
  }
}
  • docker_container: Defines the Docker container resource.

  • image: Refers to the Docker image created earlier (docker_image.my_nginx.name).

  • ports: Exposes port 80 externally.

Setting Up the Environment

Installing Docker

If Docker is not installed, you can use the following commands to install and set it up:

sudo apt-get update
sudo apt-get install docker.io
sudo chown $USER /var/run/docker.sock
docker --version

Step 3: Initialize Terraform

To set up Terraform for the first time, run the following command to download the necessary provider plugins:

terraform init

Step 4: Plan the Terraform Changes

Next, run the terraform plan command to review the changes Terraform will make based on the configuration:

terraform plan

Step 5: Apply the Terraform Configuration

After reviewing the plan, apply the configuration with the following command to create the Docker container:

terraform apply

Step 6: Verify the Docker Container

After applying the configuration, you can check if the Nginx container is running using:

docker ps

Navigate to the public IP of your server to see the default Nginx welcome page.

Step 7: Destroy the Terraform Resources

Finally, you can use the terraform destroy command to remove all the resources created by Terraform:

terraform destroy

Conclusion

In this blog post, we explored how to use Terraform to manage Docker containers by creating and managing a Docker image and a Docker container for Nginx. We also touched on the different types of blocks and resources used in Terraform, including providers and resources, which are essential for automating infrastructure.

With Terraform and Docker, managing infrastructure becomes seamless, enabling DevOps engineers to focus on scaling and automating application deployments.

Try it out for yourself and share your experience! ✨

~Tushar RanjanπŸ™‚

Β