Multi-Stage Docker Builds for a Node App With a Headless Browser Dependency
Installing a headless browser for server-side PDF generation can quietly triple your image size if you install it in the wrong stage.
The first version of almost every Dockerfile I write is a single FROM line, a COPY, an npm install, and a CMD. That works fine right up until the app needs something heavier than a runtime dependency — a headless browser for server-side PDF generation, in my case — and the image balloons from a few hundred megabytes into well over a gigabyte because the build tools, caches, and browser download all end up baked into the final layer.
Build stage stays fat, runtime stage stays lean — Chromium ships once, not twice.
The technique, not the app
This is a generic pattern, illustrated with a common real-world trigger: using something like Playwright's bundled Chromium server-side, for example to render HTML to PDF. The specifics don't matter — the same shape applies to any Node app that needs a heavy native dependency at build time, run time, or both.
# ---- Stage 1: build ----
FROM node:20-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# only needed if the browser must run in THIS stage too (e.g. to
# pre-render something during the build)
RUN npx playwright install --with-deps chromium
# ---- Stage 2: runtime ----
FROM node:20-slim AS runtime
WORKDIR /app
ENV NODE_ENV=production
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
COPY --from=build /root/.cache/ms-playwright /root/.cache/ms-playwright
CMD ["node", "dist/server.js"]What actually gets thrown away
The build stage accumulates things the runtime image never needs: the TypeScript compiler, dev dependencies, source maps for tools you only run once, and any temporary build cache. npm ci in the build stage installs everything in package.json; a production install in the runtime stage (or a targeted COPY --from=build) keeps only what actually executes.
The headless browser is the interesting case because it is neither a pure build tool nor a pure runtime dependency — if PDF generation happens at request time, the browser binary has to survive into the runtime image, but the installer and its own dependency chain do not need to. Copying just the resolved browser cache directory, rather than re-running the installer in the final stage, avoids paying for it twice.
The failure mode this avoids
Without the split, every docker build re-downloads and reinstalls browser binaries and dev tooling into a single, ever-growing layer, and the final image ships all of it to production — slower deploys, slower cold starts on autoscaled infrastructure, and a bigger attack surface from tools that were only ever needed at build time.
The number that convinced me
On the app I applied this to, moving from a single-stage build to this two-stage split cut the final image by roughly 60%. Nothing about the running application changed — the PDF generation still worked identically — only the fat that shipped alongside it disappeared. That is the whole case for multi-stage builds: pay the cost of heavy dependencies once, in a stage nobody ships.
Series: Building and Shipping.