DevOps / Scripts dockerfile Advanced

Python Multi-stage Dockerfile

Minimal production Python image using multi-stage build — under 120 MB final size.

dockerfile
FROM python:3.12-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir --prefix=/install -r requirements.txt

FROM python:3.12-slim
WORKDIR /app
COPY --from=builder /install /usr/local
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

Prerequisites

  • Docker >= 20
  • Python project with requirements.txt

Pro Tips

  • → Add USER appuser to run as non-root.

Was this module useful?

Share your experience — it helps the team improve.

Leave Feedback

Commercial Licensing

Acquire a production access token or template license for enterprise deployment.

Module Info

Language
dockerfile
Level
Advanced
Read time
3 min
Published
Jul 2025
License
Commercial License

Newsletter

New modules every week, directly to your inbox.