Files
handler/Dockerfile
T
Claude 82183d9bca Stop tracking the built web UI; build it in the Docker image instead
src/handler/api/static/ was a committed build artifact: Next's content-hashed
chunk names churn on every build, so any two branches touching frontend/ were
guaranteed merge conflicts there, PR diffs drowned in generated churn, and a
forgotten `npm run export` could silently ship a UI older than its source.

- gitignore the export (plus frontend/out and .next were already covered) and
  remove the 52 tracked files.
- Dockerfile grows a `ui` stage (npm ci + npm run build) whose output is copied
  into the packaged tree before pip install, so the image published by docker.yml
  always carries a UI built from exactly that commit's source — the frontend
  build is now effectively part of CI with no new workflow.
- .dockerignore excludes frontend artifacts and any stale local export: COPY
  into src/handler/api/static merges, so a checkout copy must never leak in.
- pyproject: hatchling skips VCS-ignored files, so `artifacts` re-includes the
  export when present; absent it, the wheel builds fine and the API just runs
  headless (it only mounts static/ when the directory exists).
- README documents the two build paths (Docker stage vs `npm run export` for
  source installs) and the headless fallback.

Verified: wheel with the export present ships all 52 files (memory page
included); wheel without it builds clean and create_app() skips the UI mount.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WYqkoYPX8NAo1V2KyXr1pk
2026-08-05 15:43:19 +00:00

66 lines
2.3 KiB
Docker

# syntax=docker/dockerfile:1
# ---- ui stage: build the dashboard's static export ----
# The export is a generated artifact (gitignored), so the image builds it here rather
# than trusting the checkout to carry it. package*.json is copied alone first so the
# npm ci layer caches until the lockfile actually changes.
FROM node:20-slim AS ui
WORKDIR /ui
COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci --no-audit --no-fund
COPY frontend ./
RUN npm run build
# ---- build stage: install the package + deps into an isolated venv ----
FROM python:3.11-slim AS builder
ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
WORKDIR /build
COPY pyproject.toml README.md ./
COPY src ./src
# Drop the built UI into the packaged tree before pip install: hatchling ships every
# non-.py file under src/handler, so the wheel carries the export and FastAPI serves it
# same-origin — exactly what committing src/handler/api/static used to provide.
COPY --from=ui /ui/out ./src/handler/api/static
RUN pip install .
# ---- runtime stage ----
FROM python:3.11-slim
ENV PATH="/opt/venv/bin:$PATH" \
PYTHONUNBUFFERED=1 \
# SQLite fallback lives on the /var/lib/handler volume; point DATABASE_URL at
# Postgres for real deploys (see .env.example / docker-compose.yml).
DATABASE_URL="sqlite:////var/lib/handler/handler.db" \
PROJECTS_ROOT="/var/lib/handler/projects"
COPY --from=builder /opt/venv /opt/venv
# Alembic runs from /app: alembic.ini resolves script_location=src/handler/migrations
# relative to the cwd, so the migration tree is shipped alongside the installed package.
WORKDIR /app
COPY alembic.ini ./
COPY src/handler/migrations ./src/handler/migrations
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
RUN useradd --system --home-dir /var/lib/handler --create-home handler \
&& mkdir -p /var/lib/handler/projects \
&& chown -R handler:handler /var/lib/handler \
&& chmod +x /usr/local/bin/docker-entrypoint.sh
USER handler
VOLUME /var/lib/handler
EXPOSE 8000
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8000/health', timeout=4)"
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["uvicorn", "handler.api.app:app", "--host", "0.0.0.0", "--port", "8000"]