devops docker IT

Automatic cache invalidation in Dockerfile

Sometimes, you would like docker to not be too smart with regards to the caching done by docker build…

I have a utility image built by my ci server whenever I push updates to a locally developed gem. The ci-system pushes the gem to the local gemserver, but whenever the utility image is built, it believes that it has already performed the step

RUN gem install gem_name

…and I will end up with a utility image containing an old version of the gem. As I do not want to build with the –no-cache option (that would force a full rebuild of the image), and I do not want to rely on manually updating any files ADDed in the Dockerfile, I ended up doing the following:

As part of the build step, my ci server now generates the file invalidate_cache by the following command:

head -n 500 /dev/urandom|md5 > invalidate_cache

…and in my Dockerfile I do a

ADD invalidate_cache /invalidate_cache

just before the gem installation command. As invalidate_cache will always have new content, docker will invalidate all caches after this step, and I get a properly updated image created each time.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.