Day 62- 90DaysOfDevOps

Terraform and Docker

Day 62- 90DaysOfDevOps

Hey Learners! Welcome back. Hope you understood the purpose of Terraform and the commands used in Terraform to provision infrastructure in the last two challenges Day60 and Day61. In this challenge, we'll dive deeper to provision and manage our Docker resources efficiently with Terraform. Let's start...

Steps:-

1- Write Blocks in the Terraform script

  • Terraform Block

  • Provider Block

  • Resource Block

2- Terraform Commands to provision resources

  • terraform init

  • terraform plan/apply

3- Accessing the Container

Pre-requisite:-

Docker should be installed on the machine with appropriate permissions. Otherwise, you'll get the below error.

1- Write Blocks in the Terraform script

We have to write a Terraform script (configuration file) which includes Terraform Block, Provider Block, and Resource Block.

Terraform Block:- In this block, we specify a provider with a specific source and version.

We are using Docker as a provider so we set up the Terraform Block as follows in main.tf directly. (You can make the terraform.tf file as a separate file as well)

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}

Provider Block:- Provider is a plugin used to create and manage resources. You can use provider block to define multiple configurations for the same provider optionally. Terraform assumes an empty default configuration for any provider that is not explicitly configured.

Add provider block for docker as follows-

provider "docker" {}

Resource Block:- This block defines components of your infrastructure. These components could be physical or virtual, such as a Docker container.

a- Resource Block for Docker Image:-

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

b- Resource Block for Docker Container:-

resource "docker_container" "nginx_cont" {
  image = docker_image.nginx.name
  name  = "nginx-cont1"
  ports {
    internal = 80
    external = 80
  }
}

The whole main.tf file would look like-

terraform {
  required_providers {
    docker = {
      source = "kreuzwerker/docker"
      version = "3.0.2"
    }
  }
}

provider "docker" {}

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

resource "docker_container" "nginx_cont" {
  image = docker_image.nginx.name
  name  = "nginx-cont1"
  ports {
    internal = 80
    external = 80
  }
}

2- Terraform Commands to provision resources

1- Initialize Terraform:-

Make sure you are in the directory where our main.tf file is written/located. Run the terraform init command to initialize Terraform and download the required provider plugins.

2- Plan and Apply the Configuration:-

User terraform plan command to view the changes Terraform will apply to provision infrastructure. Once you have reviewed the plan and are ready to apply the changes run the terraform apply command.

3- Accessing the Container

Check the container is created by running docker ps command.

Use the curl http://localhost:80 command to verify that NGINX is running inside or you can directly check through the web browser as shown below.

This is how we can provision docker containers with the help of Terraform.

Use terraform destroy to destroy all the provisioned inftrastructure.

Thank you so much for taking the time to read till the end! Hope you found this blog informative.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Find me on:- Hashnode LinkedIn Github

Happy Learning!