Docker Multi-Stage Builds: Smaller Images, Faster Deploys

Docker Multi-Stage Builds: Smaller Images, Faster Deploys

Docker multi-stage builds let you use one Dockerfile with several build stages, so the tools needed to compile your application never end up in the image you ship. You build the application in a first stage that holds the compiler and dependencies, then copy only the finished result into a slim final stage. The short answer is that multi-stage builds give you small, fast and safer images without a second Dockerfile or a separate build script, and they have been the standard way to build production images since the feature arrived in Docker 17.05.

Before multi-stage builds, developers faced an awkward choice: build inside the image and ship a bloated result, or build outside Docker and lose the reproducibility that containers exist to provide. A typical compiled application needed a compiler, a package manager and hundreds of megabytes of build tools, none of which the running application ever uses. Multi-stage builds remove that trade-off entirely, and they work with the BuildKit builder that has been Docker’s default since version 23.0. If you need the basics first, our guide to cloud native computing explained covers how images fit into modern systems.

Key takeaways

  • Docker multi-stage builds use several FROM instructions in one Dockerfile, each starting a new stage.
  • The build stage holds compilers and dependencies; the final stage holds only the compiled application and its runtime.
  • COPY –from moves files between stages, so only the finished artefact crosses into the final image.
  • Smaller images pull faster, use less disk, start quicker and expose fewer vulnerabilities.
  • Named stages, a minimal base image and a non-root user are the habits that make multi-stage builds pay off.

What a multi-stage build actually is

A multi-stage Dockerfile contains more than one FROM instruction. Each FROM starts a new stage, with its own base image and its own set of layers, and stages are independent: files in one stage are invisible to the next unless you copy them across. The final stage is the one that becomes the image, so everything before it is scaffolding that is discarded once the build finishes.

The usual pattern has two stages. The first, often called the build stage, starts from a full language image, installs dependencies and compiles the application. The second, the final stage, starts from a slim runtime image and copies only the compiled output across with a COPY –from instruction. The compiler, the source code and the intermediate files never reach the final image, which is why it ends up a fraction of the size. Best for: compiled languages such as Go, Java, Rust and C#, and any application with a build step that needs tools the running service does not.

A worked example: Node.js in two stages

The pattern is easiest to see in a concrete example. A Node.js application needs its dependencies installed and its code bundled before it can run, but the running service needs only the bundled output and the Node.js runtime. A single-stage Dockerfile would ship the package manager, the build tools and the source code alongside the running application.

FROM node:22 AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine AS final
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
COPY package.json ./
USER node
CMD ["node", "dist/server.js"]

The first stage installs dependencies and runs the build, producing a dist directory. The second stage starts from a small Alpine image and copies only the built output, the production dependencies and the package file across. Naming the stages with AS makes the COPY –from references readable, and it lets you build one stage on its own for debugging. Best for: any project where the build needs more than the run, which is most real applications.

Approach Image size What ships Main drawback
Single-stage build Large Compiler, tools, source and app Bloated, slow pulls, wider attack surface
Build outside Docker Small Compiled app and runtime only Build is not reproducible in Docker
Multi-stage build Small Compiled app and runtime only Dockerfile is longer and needs care
Distroless final stage Smallest App and runtime, no shell or tools Harder to debug inside the container

Why smaller images matter

Image size is not a vanity metric; it shows up everywhere a container is used. A smaller image pulls faster, which means faster deployments, faster scaling when a new replica starts, and faster first runs for every developer on the team. On a slow network or a busy registry, the difference between a 1 gigabyte image and a 100 megabyte one is the difference between a deploy that takes minutes and one that takes seconds.

Smaller images are also safer. Every package in an image is a potential vulnerability, and build tools, compilers and shells are among the most useful things an attacker can find inside a compromised container. A final stage that holds only the runtime and the application has far less for a scanner to flag and far less for an attacker to use. Best for: production deployments, where pull speed and a small attack surface both translate directly into reliability.

A developer looking at a monitor showing layered blocks beside a code editor
Each layer in a multi-stage build belongs to one stage, and only the layers of the final stage reach the image you ship.

Choosing the final stage base image

The final stage deserves as much thought as the build stage. A slim variant of your runtime image, such as node:22-alpine, is a good default: it holds the runtime and little else, and it is maintained by the same team as the full image. An Alpine base uses the musl C library rather than glibc, which occasionally causes trouble for native modules, so test before committing to it.

Distroless images go further, shipping only the application and its runtime dependencies, with no shell, no package manager and no utilities. They are the smallest and safest option, and they are an excellent choice for compiled binaries, but they are harder to debug, because you cannot open a shell inside the container. A good middle ground is a slim Debian-based runtime image, which keeps a shell for debugging while staying far smaller than a full build image. Best for: teams that want small images without giving up the ability to inspect a running container.

Build caching and stage ordering

Multi-stage builds share Docker’s layer cache, and stage order decides how well that cache works. Put the steps that change least first: install dependencies before copying application code, so a code change does not reinstall everything. In the build stage, copy the dependency files, install, and only then copy the source and compile.

BuildKit, the default builder since Docker 23.0, adds two features worth knowing. Cache mounts let you persist a package cache between builds without shipping it in the image, which speeds up dependency installs dramatically. The –target flag lets you build one stage on its own, which is useful for running a test stage or debugging a failing build without building the whole image. For guidance on keeping clusters healthy once these images are deployed, see our guide to Kubernetes best practices. Best for: teams that build often, where cache behaviour decides whether a rebuild takes seconds or minutes.

Testing and linting as stages

Stages are not only for building and shipping. A test stage can run the test suite during the build, so a failing test fails the build before any image is pushed. A lint stage can run the linter the same way. Because stages are independent, the test dependencies never reach the final image, and the final stage stays clean.

One practical pattern is three stages: build, test and final. The build stage compiles the application, the test stage copies the build output and runs the tests, and the final stage copies only the tested artefact. If the tests fail, the build stops, and no image is produced. This turns the Dockerfile into a small pipeline, with the same reproducibility guarantees as the rest of the build. Best for: teams that want tests and checks enforced at build time, with no extra tooling.

Multi-stage build checklist

  • Name every stage with AS, so COPY –from references stay readable.
  • Copy dependency files and install before copying source code, to keep the cache working.
  • Copy only the compiled output into the final stage, never the source or the build tools.
  • Choose the smallest final base image that runs the application reliably.
  • Run the final stage as a non-root user.
  • Use a .dockerignore file so the build context stays small and secrets stay out.
  • Build with a –target flag to debug a single stage when a build fails.

Frequently asked questions

Do multi-stage builds make builds slower?

The first build can be slower, because it builds more stages. Later builds are usually faster, because each stage caches independently and unchanged stages are reused. Cache mounts in BuildKit speed up dependency installs further.

Can I use multi-stage builds with any language?

Yes. The pattern works for any language with a build step, including Go, Java, Rust, C#, Python and Node.js. It also works for static sites, where the build stage runs the site generator and the final stage holds only the static files.

What does COPY –from do?

It copies files from one build stage into another. You name the source stage and the path to copy, and only those files cross over. Everything else in the source stage is discarded when the build finishes.

Should the final stage be distroless?

Distroless is the smallest and safest choice, but it has no shell, so debugging inside the container is harder. Use it for compiled binaries you trust, and use a slim runtime image when you want to inspect running containers.

How many stages should a Dockerfile have?

Two stages cover most applications: build and final. Add a test stage when you want tests enforced at build time. More than three or four stages usually means the Dockerfile is doing work that belongs in a build pipeline instead.

Latest articles

Scroll to Top