Files
kennethobsequio 843fc38f12 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.
2026-10-29 00:00:11 +08:00

77 lines
3.6 KiB
Docker

# syntax=docker/dockerfile:1
# ─────────────────────────────────────────────────────────────────────────────
# 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.
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 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
# --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
RUN chmod +x docker-entrypoint.sh
EXPOSE 3024
ENTRYPOINT ["./docker-entrypoint.sh"]