perf(docker): slim api image from 1.44GB to 657MB

The api image shipped every build artifact pnpm produced:

  - pnpm store + caches            769 MB
  - ffprobe-static (5 platforms)   335 MB
  - devDependencies (jest et al.)  included
  - pm2, npm cache                 ~38 MB

Split the build into a deps stage that installs with --prod and drops the
pnpm store/caches in the same layer (node_modules entries are hardlinks, so
they survive), then copy only node_modules into a clean runtime stage.

ffprobe-static ships ffprobe for darwin/linux/win32 + ia32/x64 but both
services already prefer a system ffprobe via `which ffprobe`, so its binaries
are stripped and apk ffmpeg provides ffprobe instead. That also adds the
ffmpeg binary services/ffmpeg.service.js needs for the mov/mkv fast-start
remux, which the old image never had.

Drop pm2: docker's restart policy covers crashes, and node now receives
SIGTERM directly (~17 MB image, ~40 MB RSS saved on a 1.9 GB droplet).

Verified on the QAS droplet: clean build 98s (was ~3m54s deploy step),
code-only rebuild 8s, all runtime deps + sharp + typst + ffprobe resolve,
container boots to the DB handshake.

Also prune superseded layers in the deploy workflow.
This commit is contained in:
2026-10-29 00:00:11 +08:00
parent 4fde9c31a0
commit 843fc38f12
4 changed files with 66 additions and 13 deletions
+6
View File
@@ -1,6 +1,12 @@
**/node_modules **/node_modules
.git .git
.github
.gitea
.env .env
.env.*
*.log *.log
logs
coverage
**/coverage
docker-compose.yml docker-compose.yml
.dockerignore .dockerignore
+3
View File
@@ -97,4 +97,7 @@ jobs:
cp "$HOME/new_starr/.env" .env cp "$HOME/new_starr/.env" .env
docker compose -p new_starr up -d --build --remove-orphans docker compose -p new_starr up -d --build --remove-orphans
docker compose -p new_starr ps 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 REMOTE
+53 -12
View File
@@ -1,29 +1,70 @@
FROM pandoc/typst:latest-alpine AS typst # syntax=docker/dockerfile:1
FROM node:22-alpine # ─────────────────────────────────────────────────────────────────────────────
# api — production image
# Enable corepack and activate pnpm #
RUN corepack enable && corepack prepare pnpm@11.3.0 --activate # Two stages. `deps` installs production dependencies only, then the final
# stage receives just the runtime artifacts. Everything pnpm keeps for
# pm2 manages the process; sequelize-cli runs migrations on startup # itself (content-addressable store, metadata cache, npm cache) stays behind
RUN npm install -g pm2 sequelize-cli # in the build stage, so it never inflates the shipped image.
# ─────────────────────────────────────────────────────────────────────────────
# Typst — compiles certificate.typ into the downloadable PDF certificate. # Typst — compiles certificate.typ into the downloadable PDF certificate.
# Not available as an apk package, so lift the binary out of pandoc/typst. # 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 WORKDIR /app
# Workspace manifests first (cache-friendly), then a filtered install — # Workspace manifests first (cache-friendly), then a filtered install —
# pnpm needs every workspace package's manifest present to resolve the # 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 # 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 # is gitignored (not committed), so this resolves fresh rather than
# rather than installing from a frozen lockfile. # installing from a frozen lockfile.
COPY package.json pnpm-workspace.yaml ./ COPY package.json pnpm-workspace.yaml ./
COPY apps/api/package.json ./apps/api/package.json COPY apps/api/package.json ./apps/api/package.json
COPY apps/web/package.json ./apps/web/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 COPY apps/api ./apps/api
WORKDIR /app/apps/api WORKDIR /app/apps/api
+4 -1
View File
@@ -1,4 +1,7 @@
#!/bin/sh #!/bin/sh
set -e 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