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

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:
| Type | Port | Source |
| HTTP | 80 | 0.0.0.0/0 |
| SSH | 22 | 0.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 psshowsUpand ports are mapped correctly, the app is liveMost “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.



