All guides

Multi-stage Dockerfiles: smaller images with less to patch

A multi-stage build uses several FROM instructions in one Dockerfile and copies only the artifacts you need from one stage to the next. The build tools stay behind, and the final image holds the program.

Over the shoulder of someone working in a code editor on a laptop

How stages work

Each FROM instruction begins a new stage, and each can use a different base image. A stage with no name is referred to by its number, starting with 0 for the first FROM. Adding AS <name> to a FROM instruction names the stage.

COPY --from copies files from a stage, and its source path is always resolved from the filesystem root of that stage. Docker's documentation sums up the result: you copy artifacts from one stage to another and leave behind everything you do not want in the final image.

A two-stage build for Go

This is the example from Docker's documentation, unchanged:

Dockerfile

# syntax=docker/dockerfile:1
FROM golang:1.26 AS build
WORKDIR /src
COPY <<EOF /src/main.go
package main

import "fmt"

func main() {
  fmt.Println("hello, world")
}
EOF
RUN go build -o /bin/hello ./main.go

FROM scratch
COPY --from=build /bin/hello /bin/hello
CMD ["/bin/hello"]

Shell

docker build -t hello .

The first stage starts from golang:1.26, writes a one-file program and compiles it to /bin/hello. The second stage starts from scratch and copies that one file out of the stage named build.

Docker describes the result as a production image that contains the binary and nothing more. None of the build tools are included, and the Go SDK and any intermediate artifacts are left behind. The distroless project's README shows the same pattern for Go with gcr.io/distroless/static-debian13 as the final stage.

Named stages and targets

Because the COPY instruction refers to the stage by name, it does not break if the instructions in the Dockerfile are reordered later.

--target stops the build at a named stage. This command stops at the stage named build:

Shell

docker build --target build -t hello .

Docker's documentation lists three uses for a target: debugging a specific build stage, a debug stage with debugging symbols or tools next to a lean production stage, and a testing stage that is populated with test data.

Which stages run depends on the builder. BuildKit builds only the stages that the target depends on. The legacy builder processes all stages leading up to the target, including stages the target does not depend on.

Two more forms are documented:

  • A FROM instruction can use an earlier stage as its base, as in FROM builder AS build1. The new stage picks up where that stage left off.
  • COPY --from accepts an image as well as a stage. COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf copies a file from the nginx image, and the Docker client pulls the image if necessary.

Interpreted languages

The distroless project publishes this two-stage example for Node.js:

Dockerfile

FROM node:22 AS build-env
COPY . /app
WORKDIR /app

RUN npm ci --omit=dev

FROM gcr.io/distroless/nodejs22-debian13
COPY --from=build-env /app /app
WORKDIR /app
CMD ["hello.js"]

The first stage runs npm ci, npm's clean install, and the second stage copies the whole /app directory into an image that contains the Node.js runtime.

npm's documentation describes omit as the dependency types to omit from the installation tree on disk, so --omit=dev leaves the dev type out of the installation. The entrypoint of the distroless Node.js image is set to node, which is why CMD holds only the path to a .js file.

What stays out of the final image

Docker's best-practices page names build tools that a production image may not require: compilers, build systems and debugging tools. In the Go example, the Go SDK and the intermediate artifacts stay in the first stage. In the Node.js example, the dev dependencies are omitted from the installation.

The same page gives the reasons. With a smaller base image your own image gets smaller, and fewer vulnerabilities arrive through dependencies. Few dependencies in a small image can lower the attack surface considerably.

Do not pass credentials to any stage as build arguments or environment variables. Docker's Build secrets page says both persist in the final image, and it names secret mounts and SSH mounts as the way to pass them.

The .dockerignore file

A .dockerignore file in the root directory of the build context lists patterns to exclude. Files and directories that match are removed from the context before it is sent to the builder. Docker's documentation gives the benefit: unwanted files are not sent to the builder, which improves build speed.

Text

*.md
!README.md

This example from the documentation excludes all Markdown files right under the context directory except README.md. Markdown files in subdirectories are still included. Four rules of the syntax:

  • Patterns are similar to the file globs of Unix shells.
  • ** matches any number of directories, including zero.
  • A line that starts with ! makes an exception to an exclusion.
  • The last line that matches a file determines whether it is included or excluded.

Checks before you ship

  1. Name every stage, and refer to it by name in COPY --from.
  2. Run docker history on the final image. It shows how the image was built.
  3. Pin base image versions. Docker's best-practices page says tags are mutable, and that pinning an image to a digest guarantees the same image version on every build.
  4. Add a .dockerignore file that excludes the files not relevant to the build.
  5. Rebuild regularly. The same page describes an image as a snapshot taken at build time, and tells you to rebuild with updated dependencies to keep it secure.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides