Day 62: Terraform and Docker❄🐳

Day 62: Terraform and Docker❄🐳

Day#62 Of 90 Days Of DevOps Challenge

🌈Introduction

Greetings, and thank you for joining us on our Terraform adventure once more!

In this blog post, we will delve into the world of Terraform and its capabilities in orchestrating Docker containers and images.

Terraform is a remarkably flexible instrument for automating infrastructure, and when paired with Docker, it forms a potent combination for overseeing your containerized applications.

Without further ado, let's begin our exploration!

⚓Terraform's Building Blocks and Resource Management

Terraform is an open-source Infrastructure as Code (IaC) tool designed to automate and manage infrastructure resources across various cloud providers and on-premises environments.

It relies on a declarative configuration language and provides a range of building blocks and resource management features to enable users to define, create, update, and destroy infrastructure components in a structured and consistent manner.

  1. Blocks: Blocks are the structural components of a Terraform configuration file, used to define different parts of the infrastructure setup. Common block types include:

    • Provider Blocks: These configure specific cloud providers or services that Terraform will interact with, such as AWS, Azure, or Google Cloud.

    • Resource Blocks: These define the actual infrastructure objects that Terraform will create and manage. For example, an AWS EC2 instance or an S3 bucket.

    • Variable Blocks: These define variables that can be used to parameterize your configuration, allowing for flexibility and reusability.

    • Output Blocks: These define values that will be displayed as output after running terraform apply. It's useful for retrieving information about the resources you've created.

  2. Resources: Resources are the actual infrastructure elements you want to create and manage using Terraform.

    You define them using resource blocks. Examples of resources include EC2 instances, S3 buckets, load balancers, virtual machines, and more.

    When you execute terraform apply, Terraform provisions these resources according to your configuration.

This summary provides a comprehensive understanding of how Terraform works with blocks and resources.

It's a fundamental concept for anyone using Terraform to manage infrastructure as code.

📜Task-01: Create a Terraform script with Blocks and Resources

Configuring the Docker Provider in Terraform Terraform needs to be informed about the provider you want to use.

In this case, we are using Docker. To do this, you'll create a terraform block in your main configuration file with the necessary provider information.

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

The docker block specifies the Docker provider with the desired version.

📌Provider Block

The provider block configures the specified provider, in this case, docker.

A provider is a plugin that Terraform uses to create and manage your resources.

provider "docker" {}

📌Resource Blocks in Terraform

In Terraform, resource blocks are employed to specify the elements of your infrastructure. A resource could represent either a tangible or virtual element, like a Docker container, or it could denote a conceptual resource such as a Heroku application.

Each resource block consists of two strings placed before the block: the resource type and the resource name. As an illustration, the initial resource type in this case is "docker_image," and the assigned name is "Nginx."

📜Task-02: Create a resource Block for an nginx docker image

Defining Docker Resources Now that Terraform knows which provider to use, we can define Docker resources.

These resources can include Docker images, containers, and more.

Create a Resource Block for an Nginx Docker Image

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

This block defines a Docker image named "nginx" with the latest version.

Create a Resource Block for Running an Nginx Docker Container

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "Task of Day 62"
  ports {
    internal = 80
    external = 80
  }
}

This block creates a Docker container using the "nginx" image, and it maps port 80 from the container to port 80 on the host

Now, Let's see how to apply these Terraform configurations on the command line.

Assuming you have Terraform installed and with your configuration is in a file, follow these steps:

  1. Initialize Terraform and download the Docker provider

     terraform init
    

    Note: In Case Docker Is Not Installed If Docker is not installed on your system, you can install it using the following commands:

     sudo apt-get install docker.io
     sudo docker ps
     sudo chown $USER /var/run/docker.sock
    
  2. Plan the execution to see what Terraform will do:

     terraform plan
    

  3. Apply the configuration to create the Docker resources

  4. After running the apply command, Terraform will display the resources it created and any changes made.

     terraform apply
    

  5. Now, hit the public IP address, and you can see the nginx default page.

💥Conclusion

Terraform, in combination with Docker, provides a powerful way to manage your containerized applications and infrastructure as code.

By defining Docker resources in Terraform, you can easily spin up and manage containers, images, and more.

This enables you to automate and version your infrastructure, making it easier to scale and maintain your containerized applications.

In our journey through Terraform, we've only scratched the surface. There's much more to explore, and the possibilities are endless.

So, keep experimenting and building to master the art of infrastructure automation with Terraform!

Happy learning & may your DevOps journey be filled with exciting discoveries🎊

Thank you for joining us on this exciting Day 62 of the 90 Days of DevOps challenge. I hope you found the information helpful and insightful.💥

So please keep yourself updated with my latest insights and articles on DevOps 🚀 by following me on :

Hashnode: vishaltoyou.hashnode.dev

LinkedIn: linkedin.com/in/vishalphadnis