ππ Day 51 DevOps Challenge - Terraform and Docker: A Match Made in Automation Heaven πβ¨

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:
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
Plan the Changes:
Run the following command to see what changes Terraform will apply to your infrastructure:
terraform planThis will provide a summary of the actions Terraform will take. π


Apply the Configuration:
Once you've reviewed the plan and are ready to apply the changes, run the following command:
terraform applyTerraform will prompt you for confirmation. Type
yesand hit Enter.

Check if the Container is Created:
After applying the configuration, check if the Docker container (nginx) is successfully created by running:
sudo docker ps -aThis command will list all containers, and you should see the newly created container named "nginx-container". π³

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:80If 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. π




