Sub topic 4
Smaller images with multi-stage builds
This chapter assumes you can write a basic Dockerfile, know that each instruction creates a layer, and have read the previous chapter on the build cache. It covers how a multi-stage build separates build tools from the runtime image.
The problem with a single stage
A Node.js app needs a compiler, dev dependencies and source files to build. It needs none of them to run. In a single-stage Dockerfile all of that stays in the final image. The result is larger, slower to push and pull, and has more installed packages that a vulnerability scanner can flag.
How multi-stage builds work
A Dockerfile can contain several FROM instructions. Each one starts a new stage with its own filesystem. You can name a stage with AS, then copy files out of it with COPY --from=<name>. Only the last stage becomes the published image. Earlier stages are used during the build and then discarded.
FROM node:20-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]The build stage installs every dependency and runs npm run build. The final stage starts from a fresh base image. It installs only production dependencies with npm ci --omit=dev, then copies the compiled dist folder from the build stage. The TypeScript compiler, test tools and source files never reach the final image.
Why the order of instructions matters
Both stages copy package.json and package-lock.json before the rest of the source. Docker caches each layer and reuses it when its inputs are unchanged. If you copy the whole source first, any code edit invalidates the npm ci layer and reinstalls everything. With the manifests copied first, npm ci reruns only when the dependency files change.
Choosing what to copy
COPY --from copies exactly the paths you name. This is where common mistakes happen:
- Copying too little. If the runtime needs a file you did not copy, such as a config template, the container fails at start, not at build time.
- Copying too much. Copying all of
/appfrom the build stage brings backnode_moduleswith dev dependencies and defeats the purpose. - Forgetting the user.
USER noderuns the process as a non-root user. The official Node images include this user.
Building a single stage
You can stop at a named stage with --target. This is useful in CI to run tests in an intermediate stage before building the final image.
docker build --target build -t myapp:build .
docker build -t myapp:latest .Exercise
A teammate changes the final stage to COPY --from=build /app ./ to "make sure nothing is missing." What is the effect on the image, and what should they do instead?
Show answer
The image gets the whole build stage directory, including dev dependencies and source files, so it grows and the scan surface increases. They should copy only the needed outputs, such as /app/dist, and install production dependencies in the final stage.