diff --git a/.dockerignore b/.dockerignore index bfe1caa..33af491 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,12 @@ **/node_modules .git +.github +.gitea .env +.env.* *.log +logs +coverage +**/coverage docker-compose.yml .dockerignore diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 0de052b..ab3b6ea 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -97,4 +97,7 @@ jobs: cp "$HOME/new_starr/.env" .env docker compose -p new_starr up -d --build --remove-orphans docker compose -p new_starr ps + # Reclaim space: superseded image layers and build cache older than 3 days. + docker image prune -f + docker builder prune -f --filter until=72h REMOTE diff --git a/apps/api/Dockerfile b/apps/api/Dockerfile index 48be87a..65c9f29 100644 --- a/apps/api/Dockerfile +++ b/apps/api/Dockerfile @@ -1,29 +1,70 @@ -FROM pandoc/typst:latest-alpine AS typst +# syntax=docker/dockerfile:1 -FROM node:22-alpine - -# Enable corepack and activate pnpm -RUN corepack enable && corepack prepare pnpm@11.3.0 --activate - -# pm2 manages the process; sequelize-cli runs migrations on startup -RUN npm install -g pm2 sequelize-cli +# ───────────────────────────────────────────────────────────────────────────── +# api — production image +# +# Two stages. `deps` installs production dependencies only, then the final +# stage receives just the runtime artifacts. Everything pnpm keeps for +# itself (content-addressable store, metadata cache, npm cache) stays behind +# in the build stage, so it never inflates the shipped image. +# ───────────────────────────────────────────────────────────────────────────── # Typst — compiles certificate.typ into the downloadable PDF certificate. # Not available as an apk package, so lift the binary out of pandoc/typst. -COPY --from=typst /usr/local/bin/typst /usr/local/bin/typst +FROM pandoc/typst:latest-alpine AS typst + +# ── Stage 1: dependencies (production only) ────────────────────────────────── +FROM node:22-alpine AS deps + +RUN corepack enable && corepack prepare pnpm@11.3.0 --activate WORKDIR /app # Workspace manifests first (cache-friendly), then a filtered install — # pnpm needs every workspace package's manifest present to resolve the # graph even though only apps/api's deps get installed here. pnpm-lock.yaml -# is gitignored (not committed), so this resolves fresh on every build -# rather than installing from a frozen lockfile. +# is gitignored (not committed), so this resolves fresh rather than +# installing from a frozen lockfile. COPY package.json pnpm-workspace.yaml ./ COPY apps/api/package.json ./apps/api/package.json COPY apps/web/package.json ./apps/web/package.json -RUN pnpm install --filter api... +# --prod skips jest / nodemon / concurrently. The pnpm store is deleted in +# the same layer: node_modules entries are hardlinks, so they survive the +# delete while ~500 MB of duplicate store files do not. +# +# ffprobe-static ships ffprobe for darwin/linux/win32 + ia32/x64 (~335 MB). +# The runtime image installs the distro ffmpeg instead (see below), which both +# services prefer via `which ffprobe`, so only the package stub is kept. +RUN pnpm install --filter api... --prod --no-frozen-lockfile \ + && rm -rf /root/.local/share/pnpm /root/.cache/pnpm /root/.cache/node /root/.npm \ + && rm -rf /app/node_modules/.pnpm/ffprobe-static@*/node_modules/ffprobe-static/bin + +# ── Stage 2: runtime ───────────────────────────────────────────────────────── +FROM node:22-alpine + +# sequelize-cli stays available for manual `db:migrate` runs on the droplet; +# the npm cache is cleared in the same layer so it never ships. +# +# ffmpeg brings ffprobe with it and is what the mov/mkv fast-start remux +# (services/ffmpeg.service.js) actually needs — ffprobe-static never shipped an +# ffmpeg binary. Costs ~100 MB unpacked (libavcodec/x265/aom), still far less +# than the 335 MB of removed ffprobe-static binaries. +RUN apk add --no-cache ffmpeg \ + && npm install -g --no-audit --no-fund sequelize-cli@^6.6.5 \ + && npm cache clean --force \ + && rm -rf /root/.npm + +COPY --from=typst /usr/local/bin/typst /usr/local/bin/typst + +WORKDIR /app + +# Symlinks inside pnpm's node_modules are relative, so copying the same +# directory layout keeps them resolving correctly. +COPY --from=deps /app/node_modules ./node_modules +COPY --from=deps /app/apps/api/node_modules ./apps/api/node_modules + +COPY package.json pnpm-workspace.yaml ./ COPY apps/api ./apps/api WORKDIR /app/apps/api diff --git a/apps/api/docker-entrypoint.sh b/apps/api/docker-entrypoint.sh index e366654..260f575 100644 --- a/apps/api/docker-entrypoint.sh +++ b/apps/api/docker-entrypoint.sh @@ -1,4 +1,7 @@ #!/bin/sh set -e -exec pm2-runtime start server.js --name "${APP_NAME:-app}" +# Docker's `restart: unless-stopped` policy handles crash recovery, so the +# process supervisor (pm2) is not shipped — it costs image size and ~40 MB RSS +# on a 2 GB droplet. node runs as PID 1 and receives SIGTERM directly. +exec node server.js