Day 62 - Terraform and Docker 🔥

Blocks and Resources in Terraform
Task-01
- Create a Terraform script with Blocks and Resources using
terraform.tf
file.Before that we need to install docker in our system.
sudo apt-get install docker.io -y && sudo apt-get install docker-compose -y && sudo chown $USER /var/run/docker.sock
- Create a file
terraform.tf
and write the providers of dockers
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 3.0.0"
}
}
}
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 is a plugin that Terraform uses to create and manage your resources. for that we need a file like
providers.tf
ormain.tf
in this case we usemain.tf
provider "docker" {}
Resource
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 in
main.tf
resource "docker_image" "nginx-image" {
name = "nginx:latest"
keep_locally = false
}
- Create a resource Block for running a docker container for nginx in
main.tf
resource "docker_container" "nginx-ctr" {
name = "nginx_container"
image = docker_image.nginx-image.name
ports {
internal = 80
external = 80
}
}
- After completing all the steps mentioned in Day 61's blog, we can use a command to run our Docker container with our IP address using the Nginx port number.
terraform init
terraform plan
terraform apply
docker ps
- Now, copy your IP address from your AWS console and paste it into a new tab with port number 80.
Subscribe to my newsletter
Read articles from Pooja Bhavani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Pooja Bhavani
Pooja Bhavani
Hi, I am Pooja Bhavani, an enthusiastic DevOps Engineer with a focus on deploying production-ready applications, infrastructure automation, cloud-native technologies. With hands-on experience across DevOps Tools and AWS Cloud, I thrive on making infrastructure scalable, secure, and efficient. My journey into DevOps has been fueled by curiosity and a passion for solving real-world challenges through automation, cloud architecture, and seamless deployments. I enjoy working on projects that push boundaries whether it's building resilient systems, optimizing CI/CD pipelines, or exploring emerging technologies like Amazon Q and GenAI. I'm currently diving deeper into platform engineering and GitOps workflows, and I often share practical tutorials, insights, and use cases from my projects and experiences. ✨ Let’s connect, collaborate, and grow together in this ever-evolving DevOps world. Open to opportunities, ideas, and conversations that drive impactful tech!