DEV Community

Tanay Jain
Tanay Jain

Posted on

From Zero to Deployment: Dockerizing a Flask + PostgreSQL App on AWS

When I started learning Docker, I had no idea I’d end up deploying a real application on a cloud server within the same month.

It ended up changing how I think about building software.


The Idea

I didn’t want to build just another static project.

I wanted:

  • A real backend
  • A real database
  • A real deployment

So I built a simple web app that tracks page visits and stores them in PostgreSQL.


Tech Stack

  • Python Flask (backend)
  • PostgreSQL (database)
  • Docker & Docker Compose
  • AWS EC2 (deployment)

Architecture

User (Internet)
   ↓
AWS EC2 Server
   ↓
Docker Engine
   ↓
Flask Container (Port 80)
   ↓ psycopg2
PostgreSQL Container
   ↓
Persistent Volume (pgdata)
Enter fullscreen mode Exit fullscreen mode

Key Concepts I Implemented

🔹 Multi-container system

Instead of running containers manually, I used Docker Compose.

docker compose up -d
Enter fullscreen mode Exit fullscreen mode

This starts the entire system in one command.


🔹 Environment variables (.env)

No secrets inside the code.

  • Cleaner
  • Safer
  • Production-ready

🔹 Health checks

Docker monitors the application continuously.

If Flask stops responding, the container is marked as unhealthy.


🔹 Auto-restart

Using:

restart: always
Enter fullscreen mode Exit fullscreen mode

If a container crashes, Docker automatically restarts it.


🔹 Optimized Dockerfile

  • Used python:3.10-slim
  • Cleared apt cache after install
  • Used --no-cache-dir for pip
  • Optimized layer ordering

Result: Image size reduced significantly (~300MB → ~160MB)


Biggest Challenge

Connecting Flask to PostgreSQL inside Docker.

The key realization:

Containers communicate using service names, not IP addresses.

DB_HOST=db
Enter fullscreen mode Exit fullscreen mode

Docker internally resolves db to the PostgreSQL container.

Once this clicked, the setup became much clearer.


Deployment

I deployed the app on AWS EC2.

Steps:

  1. Launch EC2 instance
  2. Install Docker
  3. Clone project
  4. Run Docker Compose

The application was then accessible via a public IP.


What I Learned

Docker is not just about containers.

It’s about building systems that are:

  • Reproducible
  • Isolated
  • Self-healing
  • Easy to deploy

Links


Final Thoughts

This project was my first real experience combining backend, database, and deployment into one system.

If you're learning Docker, building and deploying even a small project like this gives much more clarity than just following tutorials.

Top comments (1)

Collapse
 
tj2905 profile image
Tanay Jain

Built this while learning Docker — would love feedback or suggestions to improve 🙌