Terraform and Docker

Terraform and Docker

#90 Days of DevOps Challenge - Day 62

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

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

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

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

Firstly we need to install Docker use the below commands:

  sudo apt-get install docker.io -y

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

  docker --version

terraform init -This command will scan your tf files in that folder and install all the required automation things

terraform plan -This command will create an execution plan for terraforming, the things that will be installed, the names, and the properties added.

terraform apply -The actual execution and automation happen in this command.

Check docker container is created using the below command

docker ps

Now need to copy the public address and paste in browser so we can see the desired results

To destroy the infrastructure managed by Terraform and clean up the resources created, you can use the terraform destroy command. Make sure you navigate to the directory containing your Terraform configuration files before running the command.

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Printable, customizable thank you card templates | Canva

Did you find this article valuable?

Support Saikat Mukherjee's blog by becoming a sponsor. Any amount is appreciated!