A typical layout for an app that has a build step (TypeScript, bundler):
# syntax=docker/dockerfile:1
FROM node:24-bookworm-slim AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
FROM node:24-bookworm-slim AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:24-bookworm-slim
ENV NODE_ENV=production
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY package.json ./
USER node
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD node -e "fetch('http://127.0.0.1:3000/healthz').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
CMD ["node", "dist/server.js"]
Why:
- devDependencies, the compiler and the sources stay in the build stage; the final image gets only production node_modules and the built output;
- package files are copied before the code, so the npm ci layer is reused until the lockfile changes;
- USER node: the official image ships that user, no reason to run as root;
- CMD in exec form, so SIGTERM reaches node and it can shut down cleanly;
- the healthcheck uses fetch, which is built into Node, because slim images have no curl.
Add a .dockerignore with node_modules, .git and .env, otherwise COPY . . drags them into the build context.
Docs: https://docs.docker.com/build/building/multi-stage/