Skip to content

About

Infrastructure as Code project using Terraform to provision and manage MongoDB and Node.js web app containers locally via the Docker provider

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

Terraform Docker Infrastructure

An Infrastructure as Code (IaC) project using Terraform to provision and manage a multi-container application locally using the Docker provider consisting of a Node.js web app connected to a MongoDB database.

🔧 Tech Stack

  • Terraform — Infrastructure as Code
  • Docker — Container runtime
  • MongoDB 5.0 — Database
  • Node.js Web App — Frontend/backend application

📁 Project Structure

terraform-docker-infrastructure/ ├── .gitignore ├── main.tf ├── variables.tf ├── outputs.tf └── README.md

📦 Infrastructure Components

Network

A dedicated Docker network (app-network) is created so containers can communicate with each other by name.

MongoDB Container

  • Runs mongo:5.0 image
  • Credentials injected via Terraform variables
  • Exposed on port 27017

Webapp Container

  • Runs the Node.js web app
  • Connects to MongoDB using the container name as hostname
  • Exposed on port 3000
  • Depends on MongoDB being healthy first

🏗️ Architecture

        ┌─────────────────────────────────┐
        │         Docker (local)           │
        │                                  │

Browser │ ┌──────────┐ ┌─────────────┐ │ │ │ │ webapp │ │ mongodb │ │ │ :3000 │ │ container│──▶│ container │ │ └─────────▶│ │ │ │ │ │ │ └──────────┘ └─────────────┘ │ │ app-network │ └─────────────────────────────────┘ ▲ Provisioned by Terraform

🚀 How to Run

Prerequisites: Terraform and Docker installed.

1. Clone the repo:

git clone https://github.com/umarjay/terraform-docker-infrastructure.git
cd terraform-docker-infrastructure

2. Initialize Terraform (downloads Docker provider):

terraform init

3. Preview what will be created:

terraform plan

4. Apply — provisions the containers:

terraform apply

Type yes when prompted. The webapp URL will be printed when done.

5. Open in browser: http://localhost:3000

6. Destroy infrastructure when done:

terraform destroy

⚙️ Configuration

Variables can be customized in variables.tf or passed at runtime:

terraform apply -var="webapp_port=8080"
Variable Description Default
mongo_username MongoDB root username myusername
mongo_password MongoDB root password mypassword
webapp_port Port to expose webapp on 3000

📤 Outputs

After terraform apply, the following values are displayed:

Output Description
webapp_url URL to access the web app
mongo_container_name Name of the MongoDB container
webapp_container_name Name of the webapp container

📝 Key Terraform Concepts Demonstrated

  • Providers — Using the kreuzwerker Docker provider
  • Resources — Defining containers, images and networks as code
  • Variables — Parameterizing configuration for reusability
  • Outputs — Surfacing useful information after deployment
  • depends_on — Controlling resource creation order
  • Sensitive values — Masking credentials in terminal output

🛠️ Troubleshooting

  • Docker daemon not running — Run sudo systemctl start docker before applying
  • Port already in use — Change webapp_port variable to another port e.g. 3001
  • Container already exists — Run terraform destroy then terraform apply again

👤 Author

Umar Jalal

About

Infrastructure as Code project using Terraform to provision and manage MongoDB and Node.js web app containers locally via the Docker provider

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages