Guides
Python Flask (किंवा Django) App Containerize कशी करावी
By Ravindra Bagale, DevOps trainer · LinkedIn · Published: · Last updated:
Flask/Django + /health, pin requirements, gunicorn Dockerfile, build/run/curl — image मध्ये secrets नको.
Friends, pin requirements, run gunicorn, expose a health route. Chala mitrano — flask run is not production.
मित्रांनो, requirements pin, gunicorn, health route. Chala mitrano — flask run production नाही.
मित्रों, requirements pin, gunicorn, health route। Chala mitrano — flask run production नहीं।
Quick answer
Hold this vertical picture:
- /health app.
- pin requirements.
- Dockerfile gunicorn.
- .dockerignore; build.
- run; curl; non-root.
Language: English · मराठी · हिंदी
Flask container path
Containerize का
एक runtime.
Why: venv CI fail.
What: pinned + gunicorn.
How: health probes.
Example 1: Minimal Flask app
Type this:
mkdir -p /tmp/flaskapp && printf '%s\n' 'from flask import Flask' 'app = Flask(__name__)' '@app.get("/health")' 'def health():' ' return "ok", 200' '@app.get("/")' 'def hi():' ' return "hello"' > /tmp/flaskapp/app.py && cat /tmp/flaskapp/app.py
You will see:
What it means: Health route first — same habit as Node guides.
Example 2: requirements pin
Type this:
printf '%s\n' 'flask==3.0.3' 'gunicorn==22.0.0' > /tmp/flaskapp/requirements.txt && cat /tmp/flaskapp/requirements.txt
You will see:
flask==3.0.3
gunicorn==22.0.0
What it means: Pin versions — floating latest breaks Friday deploys.
Example 3: Dockerfile
Type this:
printf '%s\n' 'FROM public.ecr.aws/docker/library/python:3.12-slim' 'WORKDIR /app' 'COPY requirements.txt .' 'RUN pip install --no-cache-dir -r requirements.txt' 'COPY app.py .' 'EXPOSE 8000' 'CMD ["gunicorn", "-b", "0.0.0.0:8000", "app:app"]' > /tmp/flaskapp/Dockerfile && cat /tmp/flaskapp/Dockerfile
You will see:
FROM public.ecr.aws/docker/library/python:3.12-slim
What it means: gunicorn serves production; flask run is for laptop only.
Concept 1: Build/prove
tag/run/curl.
Why: curl विसरणे.
What: build; run; curl.
How: .dockerignore.
Example 4: Build image
Type this:
docker build -t flask-demo:1 /tmp/flaskapp && docker images flask-demo:1 --format '{{.Repository}}:{{.Tag}}'
You will see:
What it means: Numbered tag for rollback.
Example 5: Run and curl
Type this:
docker rm -f flask-demo >/dev/null 2>&1; docker run -d --name flask-demo -p 8000:8000 flask-demo:1 && sleep 1 && curl -s http://127.0.0.1:8000/health && echo && docker rm -f flask-demo >/dev/null
You will see:
What it means: Prove /health before Compose or Kubernetes.
Example 7: .dockerignore
Type this:
printf '%s\n' '__pycache__/' '.venv/' '*.pyc' '.git/' > /tmp/flaskapp/.dockerignore && cat /tmp/flaskapp/.dockerignore
You will see:
What it means: Keep build context small — faster CI.
Concept 2: Django/slim
WSGI; multi-stage.
Why: flask run prod नाही.
What: gunicorn; non-root.
How: collectstatic.
Example 6: Django note
Type this:
printf '%s\n' 'Django: CMD gunicorn myproject.wsgi:application -b 0.0.0.0:8000 and collectstatic in build or entrypoint'
You will see:
Django: CMD gunicorn myproject.wsgi:application -b 0.0.0.0:8000 and collectstatic in build or entrypoint
What it means: Same Dockerfile shape; different WSGI module.
Example 8: Multi-stage optional
Type this:
printf '%s\n' 'Builder installs deps; runtime copies site-packages — use when images must shrink under policy'
You will see:
Builder installs deps; runtime copies site-packages — use when images must shrink under policy
What it means: Link multi-stage guide mentally.
Example 9: Non-root user
Type this:
printf '%s\n' 'RUN useradd -u 10001 appuser && USER appuser'
You will see:
RUN useradd -u 10001 appuser && USER appuser
What it means: Accenture scans flag root containers.
Concept 3: Config
runtime env; Compose.
Why: secrets in layers.
What: ENV; -e; Compose.
How: CI health gate.
Example 10: Env for settings
Type this:
printf '%s\n' 'ENV APP_ENV=prod' '# secrets via runtime -e or orchestrator — not baked in image'
You will see:
ENV APP_ENV=prod
# secrets via runtime -e or orchestrator — not baked in image
What it means: Never COPY .env with passwords.
Example 11: Compose sketch
Type this:
printf '%s\n' 'services:' ' web:' ' image: flask-demo:1' ' ports: ["8000:8000"]' > /tmp/flaskapp/compose.yaml && cat /tmp/flaskapp/compose.yaml
You will see:
What it means: Local multi-service later — DB as another service.
Example 12: Fail if health missing
Type this:
printf '%s\n' 'CI should curl /health and fail the job on non-200'
You will see:
CI should curl /health and fail the job on non-200
What it means: Same gate as the EC2 Node guide.
Flask/Django card
1 health 2 pin 3 Dockerfile 4 dockerignore 5 build/run 6 non-root
Suppose: module not found
WORKDIR/COPY.
Suppose: reset
bind 0.0.0.0.
Ravindra Bagale's Tip
At Capgemini Python deliveries, reviewers reject images that bake .env secrets or run as root without a story.
Capgemini Python deliveries मध्ये .env secrets किंवा root without story = reject.
Capgemini Python deliveries में .env secrets या root without story = reject।
Check yourself (quiz)
Q1. prod?
gunicorn
Q2. pin?
reproducible
Q3. health?
/health 200
Q4. bind?
0.0.0.0
Q5. secrets?
no
Q6. Django?
wsgi module
Reading tracking
Mark this guide when you finish. Your browser remembers it on this device.
app → Dockerfile → build → run → curl /health.
Did you understand? Let us try it once more.
Frequently asked questions
flask run in prod?
No — use gunicorn/uvicorn.
Django static files?
collectstatic in build or entrypoint.
Alpine vs slim?
slim is a fine default for Python.
Multi-stage?
When image size policy requires.
Compose with DB?
Add a db service and env DATABASE_URL.
K8s next?
Same image; Deployment + Service + probes.