Skip to content
docktionary

Snippets / Dockerfile

Python app

A Dockerfile for a Flask/FastAPI-style Python service.

pythonflaskfastapi
Dockerfile
FROM python:3.12-slim
WORKDIR /app

# requirements.txt copied alone so this layer -- the slow one -- is only
# invalidated when dependencies actually change.
COPY requirements.txt .
# --no-cache-dir skips pip's download cache, which would otherwise sit
# in this layer doing nothing for you on a one-shot container build.
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

# python:*-slim has no non-root user by default, unlike the node images.
RUN useradd -m appuser
USER appuser

EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app:app"]