How docker cache works

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the docker category.

Last Updated: 2021-05-16

A Dockerfile runs a sequence of commands and caches the result of each one. Imagine you have ten commands: if the cache is invalidated for the sixth, then the last four are run without a cache.

This means you should trigger changes using artifacts. E.g. if you want the results of npm install to update based on package.json, ensure you COPY package.json before running the npm install.

Also, watch out for commands like RUN git clone xyz - this will get cached no matter what. In which case, get around it either by busting the cache by changing something before that point in the Dockerfile, or running docker build with --no-cache.

Another option is to automatically modify the Dockerfile each time with another script - such that the line in question becomes: RUN touch /, i.e. the filename 123467abc there being a unique id