Advanced image creation techniques
In this section, we will review some options and techniques available to speed up the building process and optimize image sizes.
In Chapter 1, Modern Infrastructure and Applications with Docker, we learned that images are a package of layers. These layers are distributed one over another, containing all the files, and the merging of all these layers gives us a distribution of files optimized for disk space reduction, using CoW filesystems. When a file from a lower layer has to be modified, it is copied to the top layer if it doesn’t exist there yet. All unmodified files are used in read-only mode. With that said, it is easy to understand that managing the CoW process correctly will help speed up image creation times.
Whenever we add new RUN
commands at the end of our Dockerfile, all previous layers will be used (unless we specify –-no-cache
); hence, the container runtime just needs to create new layers according to these new changes...