DevOps (Day: 62) Terraform and Docker

DevOps (Day: 62) Terraform and Docker

Day 62:#90DaysOfDevOpsChallenge

Devops Engineer, In Today lecture we will learn the Blocks and Resources used in Terraform.

TABLE OF CONTENTS:

Blocks and Resources in Terraform

Task-01

Create a Terraform Script with Blocks and Resources.

Task-02

Create a Resource Block for running a docker container for nginx.

Basics Definition:

Terraform needs to be hold 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

Task-01

Create a Terraform Script with Block and Numbers

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

This is a Terraform configuration block specifying the required Docker provider and its version.

In this configuration,you are specifying that your Terraform code requires the Docker provider to interact with Docker containers. The source parameter specifies the location of the provider code, Which is this case is the Kreuzweker/docker GitHub repository. The version parameter specifies the minimum version oef the provider that should be used.

You can copy the Syntax form above Diagram:-

No alt text provided for this image

This is a Terraform configuration block specifying the required Docker provider and its version Diagram.

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

No alt text provided for this image

Provider Block Diagram.

Resources:

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

Resource blocks have 2 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.

You can copy the syntax form the below diagram:-

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

Create a resource Block for running a docker container for nginx in the Terminal I highlight in white clour.

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

No alt text provided for this image

Create a resource Block for running a docker container for nginx Diagram.

After creating a Terraform configuration file (witha.tfextension) use the following Terraform commands to provision and manage your infrastructure.

Terraform init: Initializes a new or existing Terraform and working directory by downloading and installing any required providers and modules,initializing the backend and downloading any necessary plugins.

terraform init

No alt text provided for this image

Terraform Init Commad Diagram.

Terraform Plan: Generates an execution plan that shows waht actions Terraform will take to reach the desired state specified in the configuration file. This command also reports any changes that will be made to the infrastructure.

terraform plan

No alt text provided for this image

terraform plan Diagram1

No alt text provided for this image

terraform plan Diagram 2

No alt text provided for this image

terraform plan Diagram 3

Terraform apply:Executes the actions proposed in the execution plan generated by terraform plan. This command provisions and configures the infrastructure defined in the configuration file.

terraform apply

No alt text provided for this image

Terraform apply Diagram 1

No alt text provided for this image

Terraform apply Diagram 2

In case Docker is not installed use the below commands:-

sudo apt-get installdocker.io

sudo docker ps

sudo chown $USER /var/run/docker.sock

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

Check docker container is created using below command:

docker ps

No alt text provided for this image

Check docker container is created using below command Diagram

Browse public IP address, you can see nginx default page.

No alt text provided for this image

Browse public IP address, you can see nginx default page Diagram.

Thank you for reading!! Hope you find this helpful.

#day60challenge#90daysofdevops

Always open to suggestions..!!

~ Manoj Bhamidipati 🙂