- Add multi-stage Dockerfile using ghcr.io/astral-sh/uv:python3.13-bookworm-slim - Target lightweight python:3.13-slim-bookworm runtime - Add unprivileged appuser (UID 10001) and curl healthcheck on port 8000 - Add .dockerignore to exclude tests, caches, docs, and git data Refs: #6
53 lines
1.4 KiB
Docker
53 lines
1.4 KiB
Docker
# syntax=docker/dockerfile:1
|
|
# Multi-stage production container build for armonia-api
|
|
|
|
# Stage 1: Build virtual environment with uv
|
|
FROM ghcr.io/astral-sh/uv:python3.13-bookworm-slim AS builder
|
|
|
|
ENV UV_COMPILE_BYTECODE=1 \
|
|
UV_LINK_MODE=copy
|
|
|
|
WORKDIR /app
|
|
|
|
# Install dependencies using cache mount
|
|
RUN --mount=type=cache,target=/root/.cache/uv \
|
|
--mount=type=bind,source=uv.lock,target=uv.lock \
|
|
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
|
|
uv sync --frozen --no-install-project --no-dev
|
|
|
|
# Copy application sources and sync project
|
|
COPY . /app
|
|
RUN --mount=type=cache,target=/root/.cache/uv \
|
|
uv sync --frozen --no-dev
|
|
|
|
|
|
# Stage 2: Minimal runtime image
|
|
FROM python:3.13-slim-bookworm AS runtime
|
|
|
|
ENV PYTHONUNBUFFERED=1 \
|
|
PYTHONDONTWRITEBYTECODE=1 \
|
|
PATH="/app/.venv/bin:$PATH"
|
|
|
|
WORKDIR /app
|
|
|
|
# Install curl for container healthcheck
|
|
RUN apt-get update && \
|
|
apt-get install -y --no-install-recommends curl && \
|
|
rm -rf /var/lib/apt/lists/*
|
|
|
|
# Copy installed virtual environment and application code from builder
|
|
COPY --from=builder /app /app
|
|
|
|
# Non-root unprivileged container user
|
|
RUN useradd -u 1000 -m -s /bin/bash appuser && \
|
|
chown -R appuser:appuser /app
|
|
|
|
USER appuser
|
|
|
|
EXPOSE 8000
|
|
|
|
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
|
|
CMD curl -f http://localhost:8000/health || exit 1
|
|
|
|
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|