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
This commit is contained in:
1 parent
4a167ebb48
commit
168b835b6b
2 files changed
+70
No files matched your search
@@ -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/
|
||||
+52
@@ -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"]
|
||||
Reference in new issue
Block a user