Dockerizing Flask App
Docker packages your app and its dependencies into a container.
Benefits:
- consistent environments
- easier deploys
- simpler CI/CD
Minimal Dockerfile (example)
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENV PORT=8000
CMD ["gunicorn", "wsgi:app", "--bind", "0.0.0.0:8000"]FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
ENV PORT=8000
CMD ["gunicorn", "wsgi:app", "--bind", "0.0.0.0:8000"]Notes
- Use Gunicorn inside the container (not
flask runflask run) - Expose port via platform settings
- Store secrets via environment variables, not baked into the image
Common improvements
- add a non-root user
- use multi-stage builds
- add health checks
- pin dependencies
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
