Ordering instructions
It is quite interesting to know how the caching process works. A Dockerfile is processed from top to bottom, and at every instruction the Docker engine checks if there is a cached layer. If the instruction contains a cached layer, Docker will just pick it up. If the instruction is something new, a new container layer is created and even if the next line is cached, Docker will still re-build a new layer because the previous line is not cached one. Because of this, the ordering of the instructions is very important. Place the instructions that will remain constant at the top of the Dockerfile. Place the instructions that may change at the bottom of the Dockerfile. This makes sure we are using the caching mechanism efficiently. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access