liveBook · Manning

Final stage

As explained in Learn Docker in a Month of Lunches, Second Edition

The stage whose filesystem becomes the contents of the output Docker image.

The final stage is the last stage in a multi-stage Dockerfile. Its filesystem becomes the contents of the single Docker image produced by the build. section 4.1

Use the final stage to control exactly what enters the packaged application image:

  • Copy only required files from earlier build or test stages with COPY --from. section 4.1
  • Keep build tools and intermediate files out unless you explicitly copy them. section 4.1
  • Treat failures in any stage as build failures, even when the failing stage is not final. section 4.1

A final stage receiving tested output

A build stage creates /build.txt, a test stage updates it, and the unnamed final stage copies it into the output image. Running the image with CMD cat /build.txt displays the resulting file. section 4.1

Listing 4.1 A multi-stage Dockerfile
FROM diamol/base:2e AS build-stage
RUN echo 'Building...' > /build.txt

FROM diamol/base:2e AS test-stage
COPY --from=build-stage /build.txt /build.txt
RUN echo 'Testing...' >> /build.txt

FROM diamol/base:2e
COPY --from=test-stage /build.txt /build.txt
CMD ["cat", "/build.txt"]

Inspect the complete multi-stage Dockerfile showing how the final stage copies only the tested artifact. section 4.1

How Learn Docker in a Month of Lunches, Second Edition covers it

How deep it goes
Explains stage isolation, sequential execution, explicit copying, and failure propagation without detailing image-layer internals.
Examples
Uses a Dockerfile that creates, tests, and copies /build.txt through three stages.
What it assumes
Assumes basic Dockerfile and Docker image familiarity while introducing multi-stage structure and cross-stage copying.
What you can do after
Build a single Docker image containing only files explicitly copied into the final stage.
How it connects
Connects multi-stage builds with Dockerfiles, build stages, test stages, FROM, COPY --from, and RUN.

This explainer was generated by Manning's knowledge graph from the text of Learn Docker in a Month of Lunches, Second Edition. The section links point to the parts of the book it draws on.