Optimizing your build process
The Docker build process can and should be optimized. This will remove a lot of friction in the software development life cycle.
Many Docker beginners make the following mistake when crafting their first Dockerfile:
Figure 8.6 – Unoptimized Dockerfile for a Node.js application
Can you spot the weak point in this typical Dockerfile for a Node.js application? In Chapter 4, Creating and Managing Container Images, we learned that an image consists of a series of layers. Each (logical) line in a Dockerfile creates a layer, except the lines with the CMD
and/or ENTRYPOINT
keywords. We also learned that the Docker builder tries to do its best by caching layers and reusing them if they have not changed between subsequent builds. But the caching only uses cached layers that occur before the first changed layer. All subsequent layers need to be rebuilt. That said, the preceding structure of the Dockerfile invalidates –...