Introduction to Multi-Stage Dockerfiles
Multi-stage Dockerfiles are a feature that allows for a single Dockerfile
to contain multiple stages that can produce optimized Docker images. As we observed with the builder pattern in the previous section, the stages will usually include a builder state to build the executables from source code, and a runtime stage to run the executables. Multi-stage Dockerfiles
will use multiple FROM
directives within the Dockerfile
for each stage, and each stage will start with a different base image. Only the essential files will be copied selectively from one stage to the other. Before multi-stage Dockerfiles
, this was achieved with the builder pattern, as we discussed in the previous section.
Multi-stage Docker builds allow us to create minimal-sized Docker images that are similar to the builder pattern but eliminate the problems associated with it. As we have seen in the previous example, the builder pattern needs to maintain two Dockerfiles
and a...