Skip to main content

Command Palette

Search for a command to run...

πŸš€πŸ“… Day 51 DevOps Challenge - Terraform and Docker: A Match Made in Automation Heaven πŸš€βœ¨

Published
β€’3 min read
πŸš€πŸ“… Day 51 DevOps Challenge - Terraform and Docker: A Match Made in Automation Heaven πŸš€βœ¨
A

Passionate software engineering student | DevOps enthusiast | Seeking innovation and excellence in software engineering! πŸ‘¨β€πŸ’»

In today's journey through the realm of infrastructure automation, we delve into the dynamic duo of Terraform and Docker. Let's harness their powers to streamline and manage our resources efficiently. πŸ’ͺ

Terraform Provider Configuration

Terraform requires explicit instructions on the provider to use for automation. Here, we are using Docker as our provider. To do this, we set up the provider with a specific source and version in our main Terraform configuration file (main.tf). πŸ”§

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

Note: kreuzwerker/docker is shorthand for registry.terraform.io/kreuzwerker/docker. πŸš€

Provider Block

The provider block configures the specified provider, in this case, Docker. A provider in Terraform is a plugin used to create and manage your resources.

provider "docker" {}

Terraform Resource Blocks

Resource blocks define components of your infrastructure. These components could be physical or virtual, such as a Docker container, or they could be logical, like a Heroku application.

Each resource block specifies the resource type and a unique resource name.

Task-01: Creating a Terraform Script with Blocks and Resources

Let's create a Terraform script with Blocks and Resources. πŸ› οΈ

Resource Block for Docker Image (nginx)

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

Resource Block for Running a Docker Container (nginx)

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

Task-02: Running Terraform Commands

Follow these steps to initialize Terraform, plan the changes, and apply the configuration:

  1. Initialize Terraform:

    Navigate to the directory where your Terraform configuration (main.tf) is located and run the following command to initialize Terraform and download the required provider plugins:

     terraform init
    

  2. Plan the Changes:

    Run the following command to see what changes Terraform will apply to your infrastructure:

     terraform plan
    

    This will provide a summary of the actions Terraform will take. πŸ“‹

  3. Apply the Configuration:

    Once you've reviewed the plan and are ready to apply the changes, run the following command:

     terraform apply
    

    Terraform will prompt you for confirmation. Type yes and hit Enter.

  4. Check if the Container is Created:

    After applying the configuration, check if the Docker container (nginx) is successfully created by running:

     sudo docker ps -a
    

    This command will list all containers, and you should see the newly created container named "nginx-container". 🐳

  5. Check NGINX Running Inside the Container:

    To verify if the NGINX server is running inside the Docker container and accessible on your localhost, open a web browser or use a tool like curl or your web browser to access NGINX via the exposed port (port 80):

     curl http://localhost:80
    

    or open a web browser and navigate to:

     http://localhost:80
    

    If NGINX is running successfully inside the Docker container, you should see the default NGINX landing page or content.

With this step, we've confirmed that NGINX is running inside the Docker container and is accessible on your localhost through port 80.

Task-03: Docker Installation (In case Docker is not installed)

If Docker is not installed on your system, follow these steps: βš™οΈ

sudo apt-get update
sudo apt-get install docker.io -y
docker --version

With this foundation of Terraform and Docker, we're equipped to orchestrate and manage our infrastructure effectively. Stay tuned for more exciting automation adventures! πŸš€πŸ³

In summary, today's journey unveiled the symbiotic power of Terraform and Docker, enhancing resource management. Mastering the Terraform provider setup and resource blocks is key to successful automation. Execute with confidence using Terraform commands, and watch your Docker containers come to life seamlessly! πŸ”₯πŸš€πŸ³

Thanks for reading my blog!

Let's connect with each other. Here's my LinkedIn and Twitter. Feel free to reach out, and let's continue the discussion. πŸ™Œ

More from this blog

Adarsh Jha

65 posts