INDEPENDENT THINKING. REAL TECHNOLOGY.
AUTOBUSKESTANICETECHNOLOGY, IN PERSPECTIVE.
SoftwareANALYSIS & EXPLAINED

Docker Multi-Stage Builds: Decide What the Runtime Actually Needs

Separating compilation from execution can make an image smaller and easier to maintain, provided the final stage retains the dependencies, diagnostics, and update path the application needs.

Atmega8 programmer box1
Context photograph: Atmega8 programmer box1.jpg · FDominec / CC BY-SA 3.0. Image illustrates the subject; it does not represent our testing or necessarily the exact product discussed.
THE ESSENTIALS
  • The build environment and runtime environment have different jobs.
  • A small image still needs provenance, updates, and appropriate runtime permissions.
  • Test the exact final stage, including its failure and diagnostic behavior.

A build tool does not automatically belong in production

Compiling an application may require a compiler, development headers, package managers, and source files. Running the resulting application can require much less. A multi-stage container build makes that distinction explicit by using one stage to produce artifacts and another to contain the runtime. The opportunity is more than a smaller download. It is a chance to review what the deployed process actually depends on and remove accidental carryover from the environment in which the software was assembled.

Docker documents multi-stage builds as a way to use separate stages and copy selected outputs into a final image. Its build guidance also addresses maintainability, dependency choices, and reproducibility. These are mechanisms for controlling image composition; they do not establish that the copied application is correct or trustworthy. The practical question is whether the final stage contains the right things for the intended service, with a clear explanation of why each retained component is present.

Start with the executable contract

For a hypothetical document service, list what the process needs after startup: its executable, runtime libraries, certificate store, configuration, fonts, templates, and writable directories. A successful compilation does not prove that all these resources reached the final image. Applications can fail only when they encounter a particular language, output format, or remote service. An image that passes a simple health check may still be missing a font or trust root needed by a real request.

Separate immutable application material from deployment-specific configuration and user data. Copying a development directory wholesale can accidentally include local credentials, test fixtures, or caches. Conversely, copying only one binary can omit required runtime resources. The right boundary comes from the application’s contract, not from a universal rule that every image should contain the fewest possible files. A deliberate inventory lets reviewers understand the tradeoff and gives maintainers a reference when a future dependency changes the runtime requirements.

Build dependencies need their own provenance

A clean final stage can conceal a complicated build stage. If compilation downloads unpinned dependencies or runs scripts from an uncontrolled source, removing those tools afterward does not undo their influence on the produced artifact. Review the build inputs and how they are selected. Lockfiles, source revisions, base-image identity, and supported provenance mechanisms can make the process more traceable. The final image’s small size is not evidence that the earlier steps were appropriately controlled.

Keep secrets out of copied layers and ordinary build arguments when those mechanisms can retain or expose them. Use the platform’s documented secret-handling features for legitimate private dependencies, then verify what reaches the output. A secret absent from the final filesystem may still have appeared in a build log or an intermediate artifact. The build pipeline deserves its own information-handling review because it can access valuable credentials while producing the software that later runs with much narrower permissions.

The final stage is the product under test

Tests run in the build stage can establish useful properties of the source, but they do not exercise the final image’s exact environment. Add a focused test of the runtime artifact. Start it with representative configuration, perform a real application operation, and check shutdown behavior. Include the resources identified in the runtime inventory. The test should answer whether the packaged service works, rather than merely repeating a unit test that already passed before packaging.

Also inspect permissions. A directory writable during compilation may not be writable by the runtime identity, and that may be intentional. Define which paths the application may change and keep the rest read-only where the deployment supports that design. Avoid repairing a permission error by granting broad write access without understanding the failing operation. The error may reveal that the application is trying to store mutable data beside its executable, which is a design concern worth addressing directly.

Diagnostics should be planned, not accidentally inherited

Removing shells and package managers can reduce the material available in the running image, but operators still need a supported way to investigate failures. Decide how logs, health information, traces, and approved diagnostic tools will be obtained. A minimal runtime can be practical when the platform provides an appropriate diagnostic path. It can be frustrating when the only recovery instructions assume tools that were deliberately removed. Document the operational method with the image design rather than discovering the mismatch during an outage.

Do not retain every development utility merely to avoid planning diagnostics. Equally, do not remove useful runtime components solely to achieve a smaller size figure. The decision should reflect the service’s risk model and support requirements. A temporary diagnostic environment may be appropriate, but its access and lifecycle need control. The production image and the diagnostic process are complementary parts of the operating model. Each should have a clear owner and a reason for the authority it receives.

Base images and copied libraries still require updates. A multi-stage build can make the final dependency set clearer, but it does not maintain that set automatically. Track the base identity, rebuild when relevant fixes are available, and rerun the runtime checks. If the build uses a stable digest for reproducibility, establish a deliberate process for moving to a newer approved digest. Reproducibility should preserve knowledge of what was built, not become a reason to retain an obsolete dependency indefinitely.

Review architecture-specific outputs too. A build that copies the correct artifact for one processor can still package an incompatible result for another, so each supported target deserves an explicit packaging check.

Evaluate the boundary, not only the size

A useful before-and-after review can compare transferred bytes, startup behavior, retained dependencies, runtime identity, and the ability to investigate an ordinary failure. Keep the workload and deployment conditions comparable. If the smaller image starts faster because a different application build was used, the result needs that qualification. The strongest outcome is an image whose contents and behavior are easier to explain, with measured efficiency benefits where those benefits matter to the service.

Multi-stage builds are valuable because they make a natural software boundary visible: the tools that create an artifact need not all accompany it into execution. Use that boundary to review dependencies, provenance, permissions, and operations. Test the final stage as the artifact users will actually run. A successful result is not simply an impressively small container. It is a maintainable runtime that contains what the application needs and excludes what it inherited only by accident.

Sources & further reading

Primary references inform the reporting. Our explanations, scenarios, and conclusions are editorial analysis; source statements are not independent test results.

  1. Docker: Multi-stage buildsdocs.docker.com
  2. Docker: Building best practicesdocs.docker.com

Source publication or reference date: 2026-11-04.

See an error or an important update? Contact the editors.

AUTOBUSKESTANICE Editorial Desk

Source-backed technology explanations with a focus on engineering, practical decisions, and the limits of the evidence. How we work.

Keep exploring.