Day 62 - Terraform and Docker ๐Ÿ”ฅ

Nilkanth MistryNilkanth Mistry
3 min read

Hello, DevOps enthusiasts! Welcome to Day 62 of the #90DaysOfDevOpsChallenge. Today, we're diving into Terraform and Docker. Let's break down the concepts and steps to get hands-on with these tools on AWS. ๐ŸŒŸ

Terraform and Docker Overview ๐Ÿ› ๏ธ

Terraform is an Infrastructure as Code (IaC) tool that allows you to define and provision infrastructure using a high-level configuration language. ๐ŸŒ

Docker is a platform that enables developers to create, deploy, and run applications in containers, which are lightweight and portable environments. ๐Ÿณ

To manage Docker resources with Terraform, you need to specify the provider (in this case, Docker) and define the resources you want to create and manage. Let's get started! ๐Ÿš€

Step 1: Install Terraform and Docker ๐Ÿ–ฅ๏ธ

Before we begin, ensure you have Terraform and Docker installed on your machine. If not, you can install them using the following commands:

Install Terraform:

sudo apt-get update
sudo apt-get install -y gnupg software-properties-common curl
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update
sudo apt-get install terraform
terraform -version

Install Docker:

sudo apt-get update
sudo apt-get install docker.io

Step 2: Define the Terraform Configuration ๐Ÿ“œ

  1. Create a new directory for your Terraform project:

     mkdir terraform-docker-demo
     cd terraform-docker-demo
    

    ๐Ÿ“ Directory created!

  2. Create a new file named main.tf:

     touch main.tf
     vim main.tf
    

    ๐Ÿ“ File created!

  3. Add the Terraform block to specify the Docker provider:

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

    ๐Ÿงฉ Configuration added!

Step 3: Configure the Docker Provider โš™๏ธ

  1. Add the provider block in main.tf:

     provider "docker" {}
    

    ๐Ÿ”ง Provider configured!

Step 4: Define Docker Resources ๐Ÿ“ฆ

  1. Create a resource block for the Nginx Docker image:

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

    ๐Ÿ–ผ๏ธ Nginx image resource defined!

  2. Create a resource block for running an Nginx Docker container:

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

    ๐Ÿ“ฆ Nginx container resource defined!

Your main.tf file should now look like this:

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 = "nginx:1.19.10"
  name  = "tutorial"
  ports {
    internal = 80
    external = 80
  }
}

Step 5: Initialize and Apply Terraform Configuration ๐Ÿ’ป

  1. Initialize your Terraform configuration:

     terraform init
     terraform validate
     terraform plan
    

    ๐Ÿ Initialization complete!

  2. Apply the Terraform configuration to provision the Docker resources:

     terraform apply
    

    Review the execution plan and confirm by typing yes. โœ”๏ธ

Note: In case Docker is not installed ๐Ÿš‘

If Docker is not installed, use the following commands:

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

Summary ๐Ÿ“š

Today, we've covered how to:

  • Specify the Docker provider in Terraform.

  • Define resources for Docker images and containers.

  • Initialize and apply Terraform configurations to manage Docker resources.

Keep practicing and happy learning! ๐Ÿš€


Feel free to reach out if you have any questions or run into any issues. See you tomorrow for another exciting day of the #90DaysOfDevOpsChallenge! ๐ŸŒŸ

0
Subscribe to my newsletter

Read articles from Nilkanth Mistry directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nilkanth Mistry
Nilkanth Mistry

Embark on a 90-day DevOps journey with me as we tackle challenges, unravel complexities, and conquer the world of seamless software delivery. Join my Hashnode blog series where we'll explore hands-on DevOps scenarios, troubleshooting real-world issues, and mastering the art of efficient deployment. Let's embrace the challenges and elevate our DevOps expertise together! #DevOpsChallenges #HandsOnLearning #ContinuousImprovement