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
