Run as Non-root

Why It Matters

By default a container's process runs as root. If an attacker breaks into the app, root inside the container makes escaping to the host and damaging mounted files much easier. Create and use an unprivileged user.

FROM python:3.12-slim
RUN useradd --create-home --uid 10001 appuser
WORKDIR /app
COPY --chown=appuser:appuser . .
RUN pip install --no-cache-dir -r requirements.txt
USER appuser
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]

Reduce Privileges Further

docker run -d \
  --read-only \
  --tmpfs /tmp \
  --cap-drop ALL \
  --security-opt no-new-privileges \
  --memory 512m --cpus 1 \
  myapi:1.0

Note: Never run containers with --privileged or mount /var/run/docker.sock unless you fully understand the risk: both give the container control of the host.