Cache Bust a Docker Build

Cache bust when you want to ensure a Docker image is built fresh. Docker is nice to cache stuff for you, but sometimes you know better.

Headless CMS Scenarios

For instance, let's say you have a headless CMS that will provide data to a static site at build time. You build the site in a Docker image.

You want to ensure that every time the build is run that the Docker image is build afresh, pulling the latest data from the CMS.

Force Rebuild

One way to accomplish this is to provide an input to the Dockerfile that is unique on every build. This could be the current timestamp or a build number.

Provide it with a build argument:

docker build --build-arg CACHEBUST=$(date +"%s") .

Then in your Dockerfile:

ARG CACHEBUST
# RUN build the site

By providing this unique argument, your build will never use the cache.