Sub topic 9
Smaller images with multi-stage builds
This chapter assumes you can write a basic Dockerfile, know how layers and build caching work, and have a Node.js app with a build step, such as TypeScript compiled into a dist folder.
Why a single-stage image gets large
A build step needs tools that the running app does not. A TypeScript compiler, a bundler, and test libraries are all dev dependencies. If you install them in the same image that you ship, they stay there. The image grows, pulls take longer, and more packages are present that could contain vulnerabilities.
You cannot fix this by deleting files in a later RUN step. Each instruction adds a layer, and a layer that removes a file does not shrink the layers below it. The data is still in the image.
A multi-stage build solves this by using more than one FROM instruction. Each FROM starts a new stage with a clean filesystem. You build in one stage and copy only the results into the last one. Everything else is discarded when the build finishes.
A two-stage Dockerfile
FROM node:22-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:22-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
RUN npm ci --omit=dev
COPY --from=build /app/dist ./dist
USER node
CMD ["node", "dist/server.js"]Here is what each stage does.
- The first stage is named
buildwithAS build. It installs all dependencies withnpm ci, copies the source, and runsnpm run build. - The second
FROMstarts the final image from a fresh base. It installs only production dependencies withnpm ci --omit=dev. COPY --from=build /app/dist ./distcopies the compiled output from the first stage. This is the only thing the final image takes from it.USER noderuns the app as the unprivileged user, as in the earlier chapter on dropping root privileges.
The last stage is the image you get when the build finishes. Earlier stages are build-time only. They are not part of the result, and they are not pushed to a registry.
Building and comparing sizes
Build as usual. Docker runs both stages and tags the last one.
docker build -t myapp:1.0 .
docker images myappTo stop at an earlier stage, for example to debug the build environment, use --target:
docker build --target build -t myapp:build .The exact size savings depend on your project. The gain is usually largest when dev dependencies are heavy or when the build needs a full toolchain.
Caching and common mistakes
The caching rules from the previous chapters still apply inside each stage. Copying package*.json before the source keeps the install layer cached. Changing a source file reruns only the steps after COPY . ..
Watch for these errors:
- Copying the wrong path. If
distdoes not exist in the build stage,COPY --from=buildfails. Check that your build script writes where you expect. - Missing runtime files. The final stage has only what you copy into it. If the app reads a config file or a
publicfolder at runtime, copy that too. - Installing dev dependencies in the final stage. Use
--omit=devthere, or the savings disappear. - Misspelled stage names.
--from=must match the name given afterAS. You can also use a stage number, but names are easier to maintain.
Exercise
Exercise
Your build stage runs npm run build and produces /app/dist. The final stage runs npm ci --omit=dev, but the container fails at startup with "Cannot find module '/app/dist/server.js'". Which instruction is probably missing from the final stage, and why can't a RUN rm in the build stage replace the idea behind this approach?
Show answer
The final stage is missing COPY --from=build /app/dist ./dist. A new stage starts with a clean filesystem, so the build output is not there unless you copy it in. A RUN rm cannot reduce size because deleted files remain in the earlier layers of the same image. A separate stage avoids that by leaving those layers out of the final image.