teluzia.Cloud Engineer · Day 101
CHAPTER 15 · DOCKER AND CONTAINERS

Image optimization and multi-stage concepts

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Multi-stage building is using a workshop to fabricate the product and shipping only the finished product, not every workshop tool. Multiple FROM instructions define stages. A final stage can copy selected artifacts from a builder while leaving compilers, source caches, and development tools behind. Stage naming makes copying and targeted builds clearer. The final artifact still needs its necessary runtime libraries and configuration. Choose a compatible maintained base appropriate to runtime requirements. Smaller images can reduce transfer and exposure but are not automatically secure or compatible. Lock dependencies where appropriate, avoid unnecessary packages, and use a non-root runtime identity when the application supports it.

Think of it this way

Multi-stage building is using a workshop to fabricate the product and shipping only the finished product, not every workshop tool.

TOPIC 01 / 03Step by step

Multi-stage build boundaries

Multiple FROM instructions define stages. A final stage can copy selected artifacts from a builder while leaving compilers, source caches, and development tools behind. Stage naming makes copying and targeted builds clearer. The final artifact still needs its necessary runtime libraries and configuration. For example, cOPY --from=builder /out/app /app copies only the built artifact into the runtime stage.

WORKED EXAMPLE
COPY --from=builder /out/app /app copies only the built artifact into the runtime stage.

A compiled application needs a compiler, development headers, and source files to produce its executable. A builder stage contains those tools and writes the artifact to a known output directory. A later runtime stage begins from another base and copies only the needed artifact and runtime files. The builder's entire filesystem is not automatically shipped.

A stage is a build environment started by FROM. An artifact is the selected output to distribute. COPY --from transfers files from another stage. Name stages so references are understandable and refactoring does not depend on numeric ordering. Multi-stage design reduces shipped tooling, but the application may still need libraries, certificates, time-zone data, or a compatible runtime. Test the final stage rather than only the builder. An artifact compiled for a different architecture or incompatible library environment can exist at the right path yet fail immediately when the container starts.

Why this matters

COPY --from=builder /out/app /app copies only the built artifact into the runtime stage.

A common trap

Do not overlook this operational distinction: The final artifact still needs its necessary runtime libraries and configuration.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Multi-stage build boundaries

Multiple FROM instructions define stages. A final stage can copy selected artifacts from a builder while leaving compilers, source caches, and development tools behind. Stage naming makes copying and targeted builds clearer. The final artifact still needs its necessary runtime libraries and configuration. For example, cOPY --from=builder /out/app /app copies only the built artifact into the runtime stage.

WORKED EXAMPLE
COPY --from=builder /out/app /app copies only the built artifact into the runtime stage.

A compiled application needs a compiler, development headers, and source files to produce its executable. A builder stage contains those tools and writes the artifact to a known output directory. A later runtime stage begins from another base and copies only the needed artifact and runtime files. The builder's entire filesystem is not automatically shipped.

A stage is a build environment started by FROM. An artifact is the selected output to distribute. COPY --from transfers files from another stage. Name stages so references are understandable and refactoring does not depend on numeric ordering. Multi-stage design reduces shipped tooling, but the application may still need libraries, certificates, time-zone data, or a compatible runtime. Test the final stage rather than only the builder. An artifact compiled for a different architecture or incompatible library environment can exist at the right path yet fail immediately when the container starts.

Why this matters

COPY --from=builder /out/app /app copies only the built artifact into the runtime stage.

A common trap

Do not overlook this operational distinction: The final artifact still needs its necessary runtime libraries and configuration.

Base choice and dependency scope

Choose a compatible maintained base appropriate to runtime requirements. Smaller images can reduce transfer and exposure but are not automatically secure or compatible. Lock dependencies where appropriate, avoid unnecessary packages, and use a non-root runtime identity when the application supports it. For example, a dynamically linked binary needs compatible runtime libraries; copying it into an incompatible minimal image can fail at startup.

WORKED EXAMPLE
A dynamically linked binary needs compatible runtime libraries; copying it into an incompatible minimal image can fail at startup.

A team replaces its large runtime base with an extremely minimal one. Transfer size improves, but HTTPS calls fail because expected trust certificates are absent, or a dynamically linked binary cannot find its required library. Size optimization must preserve operational dependencies, not merely produce a successful image build.

A base image supplies the starting filesystem and runtime components. Dynamic linking requires compatible libraries at execution. A non-root user reduces some runtime authority but may need correctly owned directories. Check maintenance, package sources, architecture, and patch availability. Minimal images can reduce unused software yet are not automatically vulnerability-free or easier to debug. Choose a base from runtime requirements and supported operational practice. For interpreted applications, include the actual interpreter and required packages. Record what was removed and verify representative application behavior so reduced size is an observed improvement, not a change that silently discarded necessary functionality.

Why this matters

A dynamically linked binary needs compatible runtime libraries; copying it into an incompatible minimal image can fail at startup.

A common trap

Do not overlook this operational distinction: Lock dependencies where appropriate, avoid unnecessary packages, and use a non-root runtime identity when the application supports it.

Measurement and reproducibility

Measure image size, build time, startup, and functionality before and after optimization. An image that builds is not necessarily a working application. Base digests can make inputs explicit, while updates still need patch management. Preserve provenance and verify the final stage rather than test only the builder. For example, run the final image’s health check and compare its size to the original, recording both exact image identities.

WORKED EXAMPLE
Run the final image’s health check and compare its size to the original, recording both exact image identities.

Compare two versions of the final image using the same application tests. Record content identity, byte size, build duration, startup duration, and representative responses. A smaller image that starts slower or fails a necessary operation is not an unqualified improvement. Build-stage success is not evidence for the final runtime path.

A digest identifies image content. Provenance connects the artifact to its source and build inputs. A benchmark measures behavior under stated conditions. Pinning a base digest controls inputs but also pins an older artifact until deliberately updated. Establish patch refresh and scan review rather than equate immutability with permanent security. Compare like-for-like architecture and dependencies, since different platforms can produce different sizes. Preserve enough metadata to repeat the comparison. Optimizations should explain the mechanism, such as removing compilers or improving layer reuse, instead of attributing every change to a broad claim that multi-stage builds are always faster.

Why this matters

Run the final image’s health check and compare its size to the original, recording both exact image identities.

A common trap

Do not overlook this operational distinction: Preserve provenance and verify the final stage rather than test only the builder.

KEEP THESE WITH YOU

Today’s takeaways

Multi-stage build boundaries: Multiple FROM instructions define stages. A final stage can copy selected artifacts from a builder while leaving compilers, source caches, and development tools behind. Stage naming makes copying and targeted builds clearer. The final artifact still needs its necessary runtime libraries and configuration.

Base choice and dependency scope: Choose a compatible maintained base appropriate to runtime requirements. Smaller images can reduce transfer and exposure but are not automatically secure or compatible. Lock dependencies where appropriate, avoid unnecessary packages, and use a non-root runtime identity when the application supports it.

Measurement and reproducibility: Measure image size, build time, startup, and functionality before and after optimization. An image that builds is not necessarily a working application. Base digests can make inputs explicit, while updates still need patch management. Preserve provenance and verify the final stage rather than test only the builder.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed