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

Vishesh GhuleVishesh Ghule
3 min read

🚀 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

Screenshot from 2025-08-07 13-18-08

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

Screenshot from 2025-08-07 13-18-33


🔸Step 2: Install Terraform

sudo snap install terraform --classic

Screenshot from 2025-08-07 14-31-39


🔸Step 3: Initialize Terraform

  • In the same directory where main.tf is saved, run: terraform init

    Screenshot from 2025-08-07 13-19-33


🔸Step 4: Apply the Configuration (Start Container)

  • Now let’s actually provision the infrastructure. Run: terraform plan

    Screenshot from 2025-08-07 13-21-04

    Screenshot from 2025-08-07 13-21-23


🔸Step 5: Apply the Configuration (Start Container)

  • Now let’s actually provision the infrastructure. Run: terraform apply

    Screenshot from 2025-08-07 13-22-05

  • It will ask:

Do you want to perform these actions?
  Terraform will perform the following actions:
    ...
  Enter a value:
  • Type: yes

    Screenshot from 2025-08-07 13-22-27

    Screenshot from 2025-08-07 13-23-41


🔸Step 6: Check container is running:

docker ps

Screenshot from 2025-08-07 13-27-33

Open in browser:

Screenshot from 2025-08-07 13-27-47


🔸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

Screenshot from 2025-08-07 13-28-44

  • Remove docker container by typing command:
sudo docker rm -f nginx_container

Screenshot from 2025-08-07 13-44-58


🔸Step 8: Destroy the Infrastructure (Cleanup)

  • Terraform gives you full control — so let’s clean everything it created.

  • In your terminal, run:

terraform destroy

Screenshot from 2025-08-07 13-30-09

  • 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

Screenshot from 2025-08-07 13-45-12

Screenshot from 2025-08-07 13-45-25


🚀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.❤️🙌

Linkedln

Twitter

Github

0
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