Day 62 - Terraform and Docker 🔥

Day 62 - Terraform and Docker 🔥

·

4 min read

Welcome to our blog, where we dive into the fascinating world of Terraform! In this space, we will explore Blocks and Resources in Terraform, offering insights and practical guidance to help you harness the full power of this infrastructure as code tool.

In our first task, we’ll guide you through creating a Terraform script with Blocks and Resources, including the essential Provider Block, as well as a Terraform Docker script.

Then, in our second task, we’ll focus on crafting a Resource Block for an Nginx Docker image, providing you with a hands-on approach to understanding Terraform’s capabilities. Join us as we embark on this Terraform journey, and let’s learn and grow together!

Terraform needs to be told which provider to use in the automation, hence we need to give the provider name with source and version. For Docker, we can use this block of code in your main.tf

Blocks and Resources in Terraform

Terraform Blocks?

  • A block is a section within a configuration file that defines a specific object or behavior. Blocks are used to organize and configure resources, providers, variables, outputs, and other components of a Terraform configuration.

  • A block is a container for the each content and An argument assigns a value to a particular name

  • A resource block is a specific type of block used to define and manage a resource within an infrastructure provider. It describes the desired state of a resource and instructs Terraform on how to create, update, or destroy that resource. A resource block typically includes properties such as the resource type, name, and configuration options specific to the provider.

here is some examples of block and resources that will help you to understand more about the terraform blocks & resources.

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

Task-01

  • Create a Terraform script with Blocks and Resources

    create a terra_docker.tf and pass the docker provider

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

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:

Use resource blocks to define components of your infrastructure. A resource might be a physical or virtual component such as a Docker container, or it can be a logical resource such as a Heroku application.

Resource blocks have two strings before the block: the resource type and the resource name. In this example, the first resource type is docker_image and the name is Nginx.

Task-02

Create a resource Block for an nginx docker image

    resource "docker_image" "nginx" {
     name         = "nginx:latest"
     keep_locally = false
    }
  • 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
     }
    }
  • Initializes a new or existing Terraform working directory by downloading the necessary provider plugins using terraform init command.

  •   terraform init
    
  • Now execute the terraform plan command which will create an execution plan by comparing the desired state in the configuration to the current state of the infrastructure.

  •   terraform plan
    

    • Execute the terraform apply command so all the configurations get executed.It creates, modifies, or deletes resources as necessary to achieve the desired state, based on the execution plan generated by terraform plan.

        terraform apply
      

  • In case Docker is not installed on your Linux system use the below commands:

  •     sudo apt-get install docker.io -y
    
        sudo chown $USER /var/run/docker.sock
    
        docker --version
    
    • Check docker container is created using the below command:

    •   docker ps
      

      • Browse public IP address, you can see the Nginx default page.

  • Execute the terraform destroy so it will prompt for confirmation and then proceeds to delete the resources, reverting the infrastructure to its pre-Terraform state.

In Day 62 of my ’90 Days of DevOps’ journey, I delved into the exciting world of Terraform Blocks and Resources. I acquired knowledge about the crucial elements that make up Terraform scripts:

- The Provider Block, which serves as the gateway to define the cloud or infrastructure provider for our resources. It’s the starting point that sets the stage for our infrastructure management.

- The Resource Block, which I explored in depth. This component allows us to create, configure, and manage specific infrastructure resources. In this task, I put this knowledge to use by creating a Resource Block to provision an Nginx Docker image, covering key details like image name, ports, and other configurations.

I’m thrilled to share my learning journey with you, and I’m always open to questions, discussions, and insights related to DevOps, Infrastructure as Code (IAC), or infrastructure management. If you’d like to connect with me and explore these topics further, please find me on LinkedIn As Akash Singh . Your feedback and contributions are invaluable as we all strive to enhance our DevOps and infrastructure skills.

Did you find this article valuable?

Support Akash-DevOps by becoming a sponsor. Any amount is appreciated!

Â