Deploying Flask Apps
Lightweight Python WSGI apps with Gunicorn or Waitress, deploy to VPS, container, or serverless.
PlatformsIntermediate
Why
Flask's minimal core lets you add only what you need, producing small fast Python services. Gunicorn provides production WSGI serving with worker process management for reliability.
Platform-as-a-Service adoption grew 24% in 2024 as teams shift from VM management to git-push deploys. the cloud infrastructure market reached $330B in 2024, growing 22% year-over-year (Synergy Research), and PaaS layers capture the majority of new workloads.
When
Use Flask for small-to-medium Python APIs, internal tools, and prototypes. Choose FastAPI when async and auto-validation are needed.
How
1. WSGI — gunicorn app:app -w 4 -b 0.0.0.0:5000
2. Docker — python:3.12-slim, gunicorn CMD
3. Serverless — Zappa or serverless-wsgi for AWS Lambda
4. Env — FLASK_ENV=production, load from .env or env vars
5. Nginx — reverse proxy for TLS and static files
6. Gunicorn workers — (2 * CPU) + 1, or use gevent for async
7. Health check — add /health endpoint for load balancer
Example
# app.py
from flask import Flask, jsonify
app = Flask(__name__)
@app.route("/health")
def health(): return jsonify(ok=True)
@app.route("/api/data")
def data(): return jsonify(items=[1, 2, 3])
if __name__ == "__main__":
app.run()
# Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["gunicorn", "app:app", "-w", "4", "-b", "0.0.0.0:5000", "--timeout", "30"]
# gunicorn.conf.py — production tuning
bind = "0.0.0.0:5000"
workers = 5
timeout = 30
graceful_timeout = 10
Common Problems
- ⚠Using app.run() in production — development server, single-threaded, not for production
- ⚠Workers timeout on long requests — default 30s, increase or use gevent for streaming
- ⚠Large file uploads fail — Nginx client_max_body_size too small, increase in nginx config
Best Practices
- ✓Never use Flask dev server in production — always Gunicorn or Waitress with multiple workers
- ✓Use gevent or eventlet worker class for I/O-heavy apps — handles thousands of concurrent connections
- ✓Set up health checks and graceful shutdown — handle SIGTERM to drain in-flight requests before exit