Terraform and Docker

Terraform and Docker

Day 62 of 90daysofdevops

ยท

3 min read

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

  • 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 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's an example of a resource block in a Terraform configuration file defining an Nginx image:

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

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

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" {}

Create a Terraform Docker 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"
           }
        }
      }
    
  • Note: kreuzwerker/docker, is a shorthand Docker installation.

    kreuzwerker docker registry

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.

  • 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

  • Create a resource Block for an nginx docker image

      resource "docker_image" "my_nginx" {
                  name = "nginx:latest"
                  keep_locally = false
      }
    
  • Create a resource Block for running a docker container for Nginx

      resource "docker_container" "my_nginx_container" {
            image = docker_image.my_nginx.name
            name = "nginx-container"   
            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 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.


Thank You,

I want to express my deepest gratitude to each and every one of you who has taken the time to read, engage, and support my journey.

Feel free to reach out to me if any corrections or add-ons are required on blogs. Your feedback is always welcome & appreciated.

~ Abhisek Moharana ๐Ÿ™‚

ย