Transform your infrastructure using Terraform and Docker

Part-3

·

4 min read

Transform your infrastructure using Terraform and Docker

Terraform needs to be told which provider to be used 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 block

    A Terraform block is written in the HashiCorp Configuration Language (HCL) and contains a set of key-value pairs that describe the desired state of a resource or a module.

    A Terraform block starts with a block type, followed by an optional label, and ends with a closing brace. For example, a block that defines an AWS EC2 instance might look like this:

      pythonCopy coderesource "aws_instance" "example" {
        ami           = "ami-0c55b159cbfafe1f0"
        instance_type = "t2.micro"
        subnet_id     = aws_subnet.example.id
      }
    

    Terraform supports several types of blocks, including resource blocks, provider blocks, variable blocks, output blocks, and module blocks. Each block type has its syntax and set of key-value pairs.

  • Provider block

    In Terraform, a "provider block" is used to configure the details of a particular cloud provider or API that Terraform will use to manage resources.

    The provider block configures the specified provider, like docker, AWS, etc. A provider is a plugin that Terraform uses to create and manage your resources.

    A provider block is defined using the provider keyword, followed by the name of the provider (e.g., "aws" for Amazon Web Services), and any necessary configuration settings, such as access keys, region, etc.

    For example, here is a simple provider block for AWS:

      pythonCopy codeprovider "aws" {
        access_key = "my-access-key"
        secret_key = "my-secret-key"
        region     = "us-west-2"
    

Task-01

  • Create a Terraform script with Blocks and Resources.

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

    This is a Terraform config. block that specifies the required provider for interacting with the Docker software.

    The required_providers block specifies the provider requirements for the configuration.

    In this case, the provider is docker, and the version is specified as ~> 2.21.0, which means that any version of the provider between 2.21.0 and 3.0.0 will be acceptable. The source field specifies the location of the provider's source code.

    Provider block specifies docker as a provider here.

    Note: "kreuzwerker/docker" is shorthand for registry.terraform.io/kreuzwerker/docker.

  • Resource block

    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.

    A resource block is defined using the resource keyword followed by the resource type and a name for the resource. Within the block, you can specify the configuration settings for the resource using arguments and nested blocks.

    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.

    Create a resource Block for running an Nginx docker container.

      #Resource Block for nginx docker image
      resource "docker_image" "nginx" {
       name         = "nginx:latest"
       keep_locally = false
      }
    
      #Resource Block for running nginx docker container
      resource "docker_container" "nginx" {
       image = docker_image.nginx.latest
       name  = "tutorial"
       ports {
         internal = 80
         external = 80
       }
      }
    

Note: In case Docker is not installed, follow the steps shown below.

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

After creating terraform config. file having ".tf" extension and docker installed, follow the terraform commands as mentioned below for provisioning and managing the infrastructure.

  • Terraform init initializes a new or existing Terraform working directory by downloading and installing the necessary provider plugins and module dependencies.

      terraform init
    

  • Terraform plan provides an execution plan, a preview of the changes to be made before actually applying them, allowing for validation and verification of the changes.

      terraform plan
    

  • Terraform apply applies the changes specified in a Terraform configuration to the actual resources in the target infrastructure. It creates, updates, or deletes resources based on the instructions provided in the Terraform code.

      terraform apply
    

The docker container is successfully created via terraform and we can view this by using docker ps command.

And the application is running on its public IP address like this.

That's all guys, hope it will help you.

Reference: click this video for more details.


Thanks for reading the articles.

Also thanks for your valuable time.

Keep hustling and don't give up on your dreams.

Stay tuned for the next blog.