Skip to main content

Code to Cloud: Deploying a Full-Stack App with Docker, Terraform & CI/CD

Published 4 min readMarkdown

# From Code to Cloud: Deploying a Full-Stack App with Docker, Terraform & CI/CD

Building applications is one thing.  
**Deploying them reliably in production is a completely different game.**

In this blog, I’ll walk through how I built and deployed a **Hello World full-stack application** using **Django + React**, containerized it with **Docker**, provisioned infrastructure using **Terraform**, and automated everything using **GitHub Actions CI/CD** — along with the **real problems I faced and how I fixed them**.

This is not a toy tutorial. These are real DevOps lessons.

---

## 🧩 Project Overview

### Tech Stack

- **Backend**: Django (REST API)
- **Frontend**: React + Vite + TypeScript
- **Web Server**: Nginx
- **Containerization**: Docker & Docker Compose
- **Infrastructure**: AWS EC2 + Application Load Balancer (ALB)
- **IaC**: Terraform
- **CI/CD**: GitHub Actions

### High-Level Architecture

User → ALB → EC2 → Docker Compose
├── Frontend (Nginx :80 → 3000)
└── Backend (Gunicorn :8000)


---

## 🐳 Dockerizing the Application (The Right Way)

### Backend: Django with Multi-Stage Docker Build

To keep images small and secure, I used **multi-stage Docker builds**.

#### Why Multi-Stage Builds?
- Smaller image size
- Faster deployments
- No build tools in production
- Better security

### Backend Dockerfile

```dockerfile
FROM python:3.12-slim AS builder
WORKDIR /app
RUN apt-get update && apt-get install -y \
    gcc libpq-dev \
    && rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
FROM python:3.13-slim
WORKDIR /app
RUN useradd -m appuser && \
    chown -R appuser /app /home/appuser
COPY --from=builder /usr/local /usr/local
COPY . .
COPY entrypoint.sh .
RUN chmod +x /app/entrypoint.sh
USER appuser
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
EXPOSE 8000
CMD ["/app/entrypoint.sh"]

⚙️ Backend Entrypoint Script

The entrypoint ensures the backend is production-ready on startup.

#!/bin/sh
set -e

echo "Starting backend..."
python manage.py migrate --noinput || true
python manage.py collectstatic --noinput || true

exec gunicorn config.wsgi:application --bind 0.0.0.0:8000

Why this matters

  • Containers self-initialize

  • No manual DB steps

  • Works consistently across environments


🎨 Frontend: React + Vite + Nginx

The frontend uses a build once, serve fast approach.

Frontend Dockerfile

FROM node:20 AS builder
WORKDIR /app
COPY package*.json .
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80

🔗 Docker Compose – Service Orchestration

services:
  backend:
    image: krishnahogale/devops-assessment-backend:latest
    ports:
      - "8000:8000"
    environment:
      - DEBUG=false
      - ALLOWED_HOSTS=*

  frontend:
    image: krishnahogale/devops-assessment-frontend:latest
    ports:
      - "3000:80"
    depends_on:
      - backend

Key Decision

  • Use pre-built images, not build: on EC2

  • Ensures consistency between CI and production


☁️ Infrastructure Provisioning with Terraform

Terraform provisions:

  • EC2 instance

  • Application Load Balancer

  • Target Groups

  • Security Groups

Outputs

  • ec2_public_ip

  • alb_dns_name

These outputs are consumed by the CI/CD pipeline.


🔄 CI/CD Pipeline – Terraform + Docker + EC2

Pipeline Name

CI/CD Pipeline - Terraform + Docker + EC2

Pipeline Flow

  1. Provision Infrastructure using Terraform

  2. Build & Push Docker Images to Docker Hub

  3. Deploy to EC2 using SSH & Docker Compose

  4. Destroy Infrastructure on Failure

This pipeline ensures:

  • Fully automated deployments

  • Zero manual EC2 setup

  • Cost safety using auto-destroy


🚨 Troubleshooting: Real Issues I Faced

❌ Issue 1: Backend Container Exiting Immediately

Cause

  • Used python manage.py runserver in container

Fix


❌ Issue 2: Database Migrations Not Applied

Cause

  • Migrations not executed inside container

Fix

python manage.py migrate --noinput || true

❌ Issue 3: Docker Image Size Too Large

Cause

  • Build tools present in runtime image

Fix

  • Implemented multi-stage Docker builds

❌ Issue 4: Frontend Not Loading via Nginx

Cause

  • Incorrect build artifact copy

Fix

COPY --from=builder /app/dist /usr/share/nginx/html

❌ Issue 5: Docker Compose Rebuilding Images on EC2

Cause

  • Used build: instead of image:

Fix

  • Switched to Docker Hub images

❌ Issue 6: Permission Denied for Entrypoint Script

Cause

  • Script not executable

Fix

chmod +x entrypoint.sh

❌ Issue 7: ALB Returning 503 Service Temporarily Unavailable

Symptoms

http://98.xx.xx.xx:3000      ✅ Works
http://alb-dns.amazonaws.com ❌ 503

Root Causes

  • Target group port mismatch

  • Incorrect health check path

  • Missing ALB → EC2 security group rule

Fix

  • Target group port → 3000

  • Health check path → /

  • Updated security groups


❌ Issue 8: Docker Not Installed on Fresh EC2

Fix

  • Installed Docker automatically during deployment

❌ Issue 9: CI/CD Failure Left AWS Resources Running

Fix

  • Added destroy-on-failure job in GitHub Actions

🧠 Key DevOps Learnings

  • ALB 503 almost always means unhealthy targets

  • Never use Django runserver in production

  • Multi-stage Docker builds are essential

  • CI/CD pipelines must handle failure paths

  • Automate cleanup to avoid AWS cost leaks

About the author

  • I am an Information Technology Analyst at NTT DATA North America, where I specialize in DevOps, automation testing, and cloud-native deployments. Over the past 3 years, I’ve built CI/CD pipelines, test automation frameworks, and Generative AI solutions that help organizations streamline their workflows and improve efficiency. My Background - I studied at the Indian Institute of Technology, Patna. - Since July 2022, I’ve been contributing to projects at NTT DATA North America. My Skills - Automation Testing: REST API and UI automation using Java, Selenium, REST Assured, TestNG. - DevOps Tools: GitHub Actions, Jenkins, Docker, Terraform, Kubernetes, Azure DevOps, AWS. - Programming: Proficient in Python and Java. - Cloud & Microservices: Deploying Python-based applications on AWS EC2, integrating with MySQL and OpenAI APIs. - Monitoring & Performance: Experienced with Datadog, Grafana, JMeter, Gatling. My Achievements - Improved test efficiency by 40% through automation frameworks. - Led Generative AI initiatives, including prompt engineering, LangChain workflows, and retrieval-augmented generation (RAG) systems. - Enhanced application performance by 10% through API call caching and load testing. My Professional Presence I am active on LinkedIn, where I have over 4,000 followers and 500+ connections

No comments yet. Be the first to comment.