კითხვაNode API Docker image is over 1 GB: how do multi-stage builds actually work?

SL Sara L. წევრი რეგისტრაცია: 17 ივლ. 2026
პოსტები: 80
#1

My Dockerfile is basically FROM node, COPY everything, npm install, npm start. The image ends up huge and I keep seeing "just use multi-stage" without much detail.

So: what goes into which stage? And do I need a HEALTHCHECK if I'm only running it with docker compose? 🙂

YS Yury Slinkin თანამშრომელი · ადმინი რეგისტრაცია: 6 ივნ. 2026
პოსტები: 94
#2

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/

TP Tomasz P. წევრი რეგისტრაცია: 30 ივნ. 2026
პოსტები: 84
#3

Two questions:
1. Why npm ci two times, in deps and in build? Is one not enough?
2. Is alpine image smaller? Maybe better than slim?

ER Evgeny Rudenko წევრი რეგისტრაცია: 14 აგვ. 2026
პოსტები: 86
#4

1. The build stage needs devDependencies to compile, the final image should not have them. You could also run npm prune --omit=dev after the build and copy that node_modules, the result is the same. Two stages are just easier to read.
2. Alpine is smaller, but it uses musl instead of glibc. Packages with native code (sharp, bcrypt, some database drivers) need different prebuilt binaries or a compiler there. Slim is the safer default; switch to alpine only after you measure the difference and run the tests on it.

SL Sara L. წევრი რეგისტრაცია: 17 ივლ. 2026
პოსტები: 80
#5

Makes sense, thanks. And the compose part: does depends_on actually wait until the database is healthy, or only until the container starts?

YS Yury Slinkin თანამშრომელი · ადმინი რეგისტრაცია: 6 ივნ. 2026
პოსტები: 94
#6

By default only until it starts. To wait for healthy, the dependency needs its own healthcheck and the condition:

services:
  api:
    build: .
    depends_on:
      db:
        condition: service_healthy
  db:
    image: postgres:17
    environment:
      POSTGRES_PASSWORD_FILE: /run/secrets/db_password
    secrets:
      - db_password
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 5s
      retries: 10

secrets:
  db_password:
    file: ./db_password.txt

The HEALTHCHECK of the API shows up in docker ps and is used by orchestrators. Note that plain Docker does not restart an unhealthy container by itself; restart policies react to the process exiting, not to the health status.

Keep db_password.txt out of git.

სწრაფი პასუხი

წერა შეუძლიათ მხოლოდ შესულ წევრებს. შესვლა