Cloud concept · Updated

Multi-Stage Builds - Multi-stage builds split artifact creation into separate environments and pass only selected outputs into the final deployable artifact.

In short

A multi-stage build separates activities such as compiling, testing, and packaging so that each can use the tools it needs. For container images, this lets the production image contain the application and its runtime dependencies without also shipping compilers, test frameworks, or build caches.

Cloud Concepts Hub →

5 key aspects of Multi-Stage Builds

Multi-stage builds use distinct environments, explicit artifact handoffs, and release checks to produce a deployable image without carrying the full build environment into production.

01
Separate build and runtime environments

Use a toolchain-rich stage to compile or package the application and a separate base to run it. Choose the runtime base for compatibility and operational needs, not just size.

02
Explicit artifact handoffs

Copy named outputs such as binaries, static assets, or production dependencies rather than whole build directories. Check that files have the ownership and permissions required by the runtime user.

03
Named targets and release gates

Name stages for purposes such as testing, debugging, and release. Make tests a dependency of the release build or enforce them separately in CI so an unused test stage cannot be skipped accidentally.

04
Build cache and dependency management

Place dependency installation before frequently changing source files when the build permits it, so source changes do not invalidate dependency layers unnecessarily. Pin dependency versions and update pinned base images deliberately.

05
Stable release identity and retention

Tag the published release for discovery and deploy by digest to select the exact image that passed verification. Retain deployable and rollback images separately from short-lived CI outputs where registry policies allow.

Cloud services for Multi-Stage Builds

A build service runs the container builder, enforces test gates, and publishes the selected release target. A registry assigns the published image a content digest and stores it for deployment. For example, AWS CodeBuild can publish a verified release image to Amazon ECR, where deployment jobs can pull that exact image by digest. Cloud Build provides a comparable build role, while Azure Container Registry can combine hosted build execution and image storage through ACR Tasks. The build definition controls what goes into the image; registry retention controls how long the published image remains available.

AWS CodeBuildAWS CodeBuild

AWS CodeBuild can run a container builder against a multi-stage Dockerfile in a suitably configured build environment. A build project can enforce test targets, build the release target, and push the resulting image to a registry.

Azure Container Registry

Azure Container Registry can store release images and, through ACR Tasks, run builds from multi-stage Dockerfiles. ACR task steps orchestrate the workflow; Dockerfile stages control which files reach the release image.

What is Multi-Stage Builds

2 min read

A multi-stage build defines separate build environments and makes the handoff between them explicit. In a Dockerfile, each FROM instruction starts a stage. A stage can use its own base image or build on an earlier stage. COPY --from transfers selected files from a named stage without inheriting its filesystem layers. The last stage is the default output, but a builder can target another named stage, for example to run tests in CI or produce a separate debugging image.

Consider a Go API that needs a compiler and test dependencies during development but only an executable and a few runtime files in production. A build stage compiles the executable, and a release stage copies it into a smaller runtime base. If the application makes outbound HTTPS requests, that base may need trusted CA certificates. If the executable is dynamically linked, the runtime must provide compatible libraries. An image can build successfully and still fail at startup if its runtime base is incompatible with the binary or if the binary targets the wrong CPU architecture.

In cloud CI, make test execution an explicit release requirement. With builders such as BuildKit, a test stage that the release stage does not depend on can be skipped when building the release target. Either make the release target depend on a stage that runs the required tests or have CI build the test target separately and block publication on failure. Also smoke-test the final image: tests that pass in the builder environment will not catch every missing file, permission problem, or startup failure in the runtime image.

Publish the verified release image to a container registry. A version or commit tag makes the image easy to find, while a digest identifies the exact published artifact. Promote that digest between environments instead of rebuilding for each environment or deploying from an unprotected mutable tag. Configure registry lifecycle policies to remove stale CI images or unused build-cache artifacts where applicable, while retaining images needed for active deployments and rollback. Intermediate stages are not automatically published as separate images, but they can still consume build-cache storage.

Smaller images can reduce transfer time and remove unnecessary software, but a minimal image can also make live debugging harder. Multiple stages add maintenance work: the build and runtime bases need updates, and native dependencies must remain compatible across them. A separate build stage is not a secret-management boundary. Use the builder's secret-mount mechanism for private dependency credentials and keep secrets out of copied outputs, build logs, and exported caches. The practical goal is an image that contains what the application needs to run, not simply the smallest possible image.

Practice Multi-Stage Builds in a full exam

Questions on Multi-Stage Builds in each practice exam, with every answer option and an explanation for each:

Start a free practice exam

Learn Multi-Stage Builds in lessons

1 short lesson on Multi-Stage Builds in 1 certification course.

Open the lessons