Docker: Smaller Images with Multi-Stage Builds
My first Dockerfile for a Node.js application produced an image of well over a gigabyte. It contained the full Node.js toolchain, every development dependency, the TypeScript compiler, the test files and the .git folder. Production needed perhaps a tenth of that.
Large images are slow to push and pull, take longer to start in a cluster, and contain more software that can have security vulnerabilities. Multi-stage builds are the standard way to fix this, and they are simpler than they sound.
The Single-Stage Dockerfile
This is roughly where I started:
FROM node:22
WORKDIR /app
COPY . .
RUN npm install
RUN npm run build
CMD ["node", "dist/server.js"]
It works, but everything used to build the application ends up in the image: the full Debian-based Node.js image, devDependencies, source files and anything else in the folder.
The Idea Behind Multi-Stage Builds
A Dockerfile can have several FROM lines. Each one starts a new stage. Later stages can copy files from earlier ones, and only the last stage becomes the final image.
So the build happens in a stage that has all the tools, and the final stage contains only what is needed to run.
A Multi-Stage Node.js Image
# ---- build stage ----
FROM node:22-slim AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
# ---- production dependencies ----
FROM node:22-slim AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci --omit=dev
# ---- final image ----
FROM node:22-slim
WORKDIR /app
ENV NODE_ENV=production
COPY --from=deps /app/node_modules ./node_modules
COPY --from=build /app/dist ./dist
COPY package.json ./
USER node
EXPOSE 3000
CMD ["node", "dist/server.js"]
The final image has the Node.js runtime, production dependencies and the compiled output. The TypeScript compiler, test libraries and source code stayed behind in stages that are thrown away.
A few details matter here:
npm ciinstalls exactly what is in the lock file and fails if it does not matchpackage.json. That is what I want in a build.- Copying
package.jsonbefore the source means the dependency layer is cached. Changing a source file does not reinstall every package. USER noderuns the application as a non-root user that the official image already provides.
Compiled Languages Benefit Even More
The difference is largest for languages that compile to a binary. A Go service can be built with the full Go toolchain and copied into an image that contains almost nothing else:
FROM golang:1.25 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /out/app ./cmd/app
FROM gcr.io/distroless/static-debian12
COPY --from=build /out/app /app
USER nonroot
ENTRYPOINT ["/app"]
The final image is often around 20 MB. Distroless images contain no shell or package manager, which also means there is very little for an attacker to use.
The same pattern works for .NET, where the SDK image builds and the much smaller ASP.NET runtime image runs:
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY *.csproj ./
RUN dotnet restore
COPY . .
RUN dotnet publish -c Release -o /out
FROM mcr.microsoft.com/dotnet/aspnet:10.0
WORKDIR /app
COPY --from=build /out .
ENTRYPOINT ["dotnet", "MyApi.dll"]
And for Java, with a JDK in the build stage and only a JRE in the final one.
Do Not Forget .dockerignore
COPY . . copies everything in the build context. A .dockerignore file keeps unnecessary and sensitive files out:
node_modules
dist
.git
.env
*.log
coverage
Without it, a local node_modules folder can overwrite the one installed in the image, and an .env file with real credentials can end up in a layer. Even if a later step deletes a file, it remains in the earlier layer and can be extracted from the image.
Choosing a Base Image
The base image of the final stage has the biggest effect on size:
| Base | Typical use | Notes |
|---|---|---|
node:22 | build stages | full Debian, many tools |
node:22-slim | most Node.js apps | Debian with fewer packages |
node:22-alpine | small images | uses musl instead of glibc, which can break native modules |
| distroless | compiled binaries, Java, Node.js | no shell, smallest attack surface |
I default to slim images for interpreted languages. Alpine is smaller, but I have lost enough time to native modules behaving differently on musl that I only use it when size really matters.
Using a Stage for Tests
Stages do not have to end up in the final image. A test stage can run the test suite, and the build can target it explicitly:
FROM build AS test
RUN npm test
docker build --target test .
In CI, this runs the tests in exactly the same environment as the build, without adding test tools to the production image.
Checking the Result
docker images shows the size, and docker history shows which layers contribute to it:
docker build -t myapp .
docker images myapp
docker history myapp
For a deeper look, the open-source tool dive lets you browse each layer and see which files were added.
Summary
- Use one stage with all the build tools and copy only the output into the final stage.
- Copy dependency manifests before source code so dependency layers are cached.
- Add a
.dockerignoreso local and secret files never enter the image. - Choose a small base for the final stage and run as a non-root user.
Moving my images to multi-stage builds made them several times smaller, and the Dockerfiles became easier to read, because each stage has one clear job.
More in DevOps
- MDDeveloper Workflow: Preparing a Software Project for a Safe Release2026-06-23
- JVJava: Writing Maintainable Integration Tests with Testcontainers2026-06-16
- JVJava: Securely Managing Secrets in Application Deployments2026-04-07
- C#ASP.NET Core: Health Checks That Reflect Real Application Readiness2026-03-06