What Is a Docker Multi-Stage Build? Separate Build Tools from Runtime
Learn how Docker build stages share selected artifacts, when this reduces the runtime image, and how to check dependencies, secrets, and target stages.
What Is a Docker Multi-Stage Build? Separate Build Tools from Runtime
A multi-stage build uses multiple FROM instructions in one Dockerfile and copies only selected outputs from an earlier stage into the final stage. This can keep compilers, development dependencies, and intermediate files out of the runtime image. It does not automatically make the image safe or every build faster: you still need the right runtime files, base image, and dependency checks.
How stages work
Each FROM starts a new stage with its own base and instructions. COPY --from=<stage> selects files from an earlier stage (or an external image) for the current stage. Files not copied are not part of the final stage filesystem. Naming a stage with AS build is clearer and more stable than referring to it by a numeric index.
A compiler may be needed to build an application but not to run it. Separating these environments makes the boundary explicit. Docker can also build a named target stage for testing or debugging with --target.
A small Go example
This example builds a program using only the Go standard library and copies it into the empty scratch base. It assumes a Docker builder that supports the Dockerfile syntax, a main.go file in the build context, and no extra runtime files or C libraries. These are documented procedure examples, not commands tested here.
# syntax=docker/dockerfile:1
FROM golang:1 AS build
WORKDIR /src
COPY main.go .
RUN CGO_ENABLED=0 GOOS=linux go build -o /out/hello .
FROM scratch AS runtime
COPY --from=build /out/hello /hello
ENTRYPOINT ["/hello"]Build and run:
docker build -t hello:local .
docker run --rm hello:localIf the example app is valid, the container exits and prints its output. You can build the intermediate stage separately:
docker build --target build -t hello:build .Verify the final image by running it and checking the expected behavior. CI can automate the build and application tests.
When scratch is not enough
scratch has no shell, package manager, CA certificates, timezone data, or user database. An application that makes HTTPS requests or needs shared libraries, templates, migration files, or user settings may fail. Copy required runtime files explicitly or choose a fuller runtime base such as an appropriate slim or distroless image. Check compatibility with native libraries instead of choosing a base only because it is small.
A multi-stage build excludes build tools from the final stage, but it does not necessarily erase build cache, intermediate images, logs, or artifacts stored elsewhere. Vulnerable libraries or incorrect permissions copied into the final stage remain. Review runtime packages, user, file permissions, certificates, and dynamic dependencies; rebuild regularly.
Separate test and debug targets
A Dockerfile can have build, test, debug, and production stages. For example, a test stage can derive from the build stage and run tests, while the final runtime stage copies only the executable. docker build --target test builds a particular target. Legacy Docker builder and BuildKit can process unrelated stages differently; BuildKit can skip stages that the selected target does not depend on.
Pass build secrets safely
Do not place API tokens or repository passwords in Dockerfile ARG or ENV. Build arguments can appear in image history or provenance metadata, and ENV persists into runtime containers. BuildKit secret mounts expose a secret to one RUN instruction as a file or environment variable.
Dockerfile:
RUN --mount=type=secret,id=token \
some-client --token-file /run/secrets/tokenBuild command, passing the client environment variable API_TOKEN:
docker build --secret id=token,env=API_TOKEN -t app:local .A secret mount cannot prevent a command from printing the secret into logs; check the tool’s output behavior as well.
What to check
| Check | How to verify |
|---|---|
| All required outputs were copied | Run the final image and exercise key paths |
| Runtime libraries and CA certificates exist | Test the actual HTTPS or native-code path |
| Secrets do not persist | Review ARG/ENV and use secret mounts |
| The intended stage is built | Check the image tag and --target; test in CI |
| The image is appropriately minimal | Review packages, permissions, and vulnerabilities—not size alone |
Multi-stage builds separate build and runtime environments and copy selected artifacts into the final stage. Verify the final image contains every runtime library, certificate, and data file it needs. Manage build cache, temporary images, and secrets separately.
Sources
[Docker: Multi-stage builds](https://docs.docker.com/build/building/multi-stage/)
[Docker: Build best practices](https://docs.docker.com/build/building/best-practices/)
[Docker: Build variables](https://docs.docker.com/build/building/variables/)
[Docker: Build secrets](https://docs.docker.com/build/building/secrets/)
[Dockerfile guide](https://knowledge.linkspot.net/ko/articles/83427e7d-a9ae-423d-a9ac-862b9bc610d6)
[Container image layers](https://knowledge.linkspot.net/ko/articles/8ffef5b5-a5e9-444c-ba6f-4e9138cfd3a0)
Scope: based on Docker Build documentation checked 2026-11-22. Builder and version can affect supported options and stage processing.