Skip to main content

Command Palette

Search for a command to run...

From Flask App to AWS EC2: Containerizing and Deploying a Web App with Docker

Published
3 min read
From Flask App to AWS EC2: Containerizing and Deploying a Web App with Docker
P
👋 Hey there! Backend-focused developer building scalable systems with Node.js & FastAPI 🚀 Working with AWS, Docker, and modern backend architectures ☁️ Passionate about performance, system design, and continuous learning ⚔️💻 Sharing my journey of building real-world backend systems 💬

Today was one of those days where things actually click.

I built a simple Flask web application, containerized it using Docker, and deployed it live on an AWS EC2 instance. No magic. No shortcuts. Just understanding how the layers fit together.

This post documents the exact end-to-end process.


What I Built Today

  • A simple Flask web app

  • A Docker image for the app

  • Ran the container on an AWS EC2 instance

  • Exposed it publicly using Security Groups

By the end, the app was accessible via a public IP on port 80.


📁 Project Structure

flask-app-ecs/
├── app.py
├── run.py
├── requirements.txt
├── Dockerfile
└── README.md

🧩 Flask Application

app.py

from flask import Flask

app = Flask(__name__)

@app.route("/")
def home():
    return "Flask app running inside Docker on AWS EC2 🚀"

run.py

from app import app

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=80, debug=True)

Key point:
0.0.0.0 allows the app to accept traffic from outside the container.


📦 Dependencies

requirements.txt

flask==2.2.2
Werkzeug==2.2.2

🐳 Dockerfile

This is the heart of containerization.

FROM python:3.9-slim

WORKDIR /app

COPY . .

RUN pip install -r requirements.txt

EXPOSE 80

CMD ["python", "run.py"]

What this does:

  • Uses a lightweight Python base image

  • Copies app code into the container

  • Installs dependencies

  • Exposes port 80

  • Runs the Flask app


🔨 Building the Docker Image

On the EC2 instance:

docker build -t flask-app .

If this succeeds, Docker creates a reusable image.


▶️ Running the Container

docker run -d -p 80:80 flask-app

Explanation:

  • -d → detached mode

  • -p 80:80 → map EC2 port 80 to container port 80

Verify it’s running:

docker ps

Expected output includes:

0.0.0.0:80->80/tcp

☁️ AWS EC2 Configuration

1. EC2 Instance

  • Amazon Linux / Ubuntu

  • Docker installed

  • Public IPv4 address assigned

2. Security Group (Critical Step)

Inbound rules:

TypePortSource
HTTP800.0.0.0/0
SSH220.0.0.0/0

Without this, the app will never be visible from the browser.


🌍 Accessing the App

Using the EC2 Public IPv4 address:

http://<EC2-PUBLIC-IP>

Seeing the Flask response in the browser confirmed:

  • Flask is running

  • Docker is working

  • EC2 networking is correct

  • Security Group is configured properly


🧠 Key Learnings

  • Docker exposes ports to the server

  • AWS Security Groups expose the server to the internet

  • If docker ps shows Up and ports are mapped correctly, the app is live

  • Most “it doesn’t work” issues are networking, not code


✅ Final Thoughts

This wasn’t just about Flask or Docker.
It was about understanding how applications move from code → container → server → internet.

Once you understand the layers, deployment stops being scary and starts being systematic.

Today was a good day.