Use a slim base image, install pinned dependencies first (for layer caching), copy the code, run as a non-root user, and start Uvicorn bound to 0.0.0.0; add a health check, and pass configuration through environment variables.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY ./app ./app
RUN useradd -m appuser
USER appuser
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]Behind a reverse proxy or load balancer (Nginx, a cloud load balancer) terminate TLS there, and use Uvicorn's proxy headers setting so client IPs and schemes are read correctly.