From 168b835b6b45b890bed23cd68b0859d518f2c743 Mon Sep 17 00:00:00 2001 From: kiskaadee Date: Mon, 19 Oct 2026 14:13:17 -0500 Subject: [PATCH] build(docker): add multi-stage containerization with uv builder - 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 --- .dockerignore | 18 ++++++++++++++++++ Dockerfile | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ebafecd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,18 @@ +.git/ +.venv/ +__pycache__/ +*.pyc +*.pyo +*.pyd +.pytest_cache/ +.ruff_cache/ +.coverage +htmlcov/ +*.db +*.sqlite +*.sqlite3 +.env +*.local.env +.direnv/ +.vscode/ +.idea/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d201ace --- /dev/null +++ b/Dockerfile @@ -0,0 +1,52 @@ +# 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"]