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.