Day 61: Creating an Nginx Docker Container with Terraform – Step-by-Step Guide

Table of contents

🚀 Introduction
In today’s DevOps world, automation is the backbone of efficient deployments. Instead of manually creating containers, wouldn’t it be great if you could define everything in a simple configuration file and let the tool do the heavy lifting?
That’s exactly where Terraform comes into play.
In this guide, we’ll learn how to use Terraform to automatically create and manage a Docker container running the latest Nginx image — all without clicking around or running multiple manual commands.
This tutorial is beginner-friendly and perfect for those who want to understand Infrastructure as Code (IaC) while working locally with Docker.
🧰 Tools Used
Terraform
Docker
Ubuntu (Local Machine)
🔸Step 1: Create Project Folder & Files
mkdir task-3-terraform-docker
cd task-3-terraform-docker
touch main.tf
- Then open it using any text editor (like nano, code, or VS Code), and paste this code inside main.tf:
terraform {
required_providers {
docker = {
source = "kreuzwerker/docker"
version = "~> 2.23.0"
}
}
}
provider "docker" {}
resource "docker_image" "nginx" {
name = "nginx:latest"
}
resource "docker_container" "nginx" {
name = "nginx_container"
image = docker_image.nginx.latest
ports {
internal = 80
external = 8081
}
}
🔸Step 2: Install Terraform
sudo snap install terraform --classic
🔸Step 3: Initialize Terraform
In the same directory where main.tf is saved, run:
terraform init
🔸Step 4: Apply the Configuration (Start Container)
Now let’s actually provision the infrastructure. Run:
terraform plan
🔸Step 5: Apply the Configuration (Start Container)
Now let’s actually provision the infrastructure. Run:
terraform apply
It will ask:
Do you want to perform these actions?
Terraform will perform the following actions:
...
Enter a value:
Type:
yes
🔸Step 6: Check container is running:
docker ps
Open in browser:
Visit:
http://localhost:8081
You should see the default Nginx welcome page.
🔸Step 7: Explore Terraform State
Terraform keeps track of all created resources using a file called terraform.tfstate.
View the state file
In terminal:
ls
You will see:
main.tf
&terraform.tfstate
List resources managed by Terraform
terraform state list
- Output will show:
docker_image.nginx
docker_container.nginx
- Remove docker container by typing command:
sudo docker rm -f nginx_container
🔸Step 8: Destroy the Infrastructure (Cleanup)
Terraform gives you full control — so let’s clean everything it created.
In your terminal, run:
terraform destroy
- It will show what resources will be destroyed:
Terraform will destroy the following:
# docker_container.nginx will be destroyed
# docker_image.nginx will be destroyed
- Then it will ask:
Do you really want to destroy all resources? (yes/no)
Type : yes
🚀Conclusion
By combining Terraform and Docker, you can create, manage, and destroy containers in a completely automated way. This not only saves time but also ensures that your infrastructure is consistent and repeatable.
This project is a simple but powerful introduction to Infrastructure as Code, and it lays the foundation for scaling into more complex setups like AWS, Kubernetes, and CI/CD pipelines.
Thanks for reading to the end; I hope you gained some knowledge.❤️🙌
Subscribe to my newsletter
Read articles from Vishesh Ghule directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Vishesh Ghule
Vishesh Ghule
I'm proficient in a variety of DevOps technologies, including AWS, Linux, Python, Docker, Git/Github, Shell Scripting, Jenkins and Computer Networking. My greatest strength is the ability to learn new things because I believe there is always room for self-development