How can I prevent a Dockerfile instruction from being cached? How can I prevent a Dockerfile instruction from being cached? curl curl

How can I prevent a Dockerfile instruction from being cached?


A build-time argument can be specified to forcibly break the cache from that step onwards. For example, in your Dockerfile, put

ARG CACHE_DATE=not_a_date

and then give this argument a fresh value on every new build. The best, of course, is the timestamp.

docker build --build-arg CACHE_DATE=$(date +%Y-%m-%d:%H:%M:%S) ...

Make sure the value is a string without any spaces, otherwise docker client will falsely take it as multiple arguments.

See a detailed discussion on Issue 22832.


docker build --no-cache would invalidate the cache for all the commands.

Dockerfile ADD command used to have the cache invalidated. Although it has been improved in recent docker version:

Docker is supposed to checksum any file added through ADDand then decide if it should use the cache or not.

So if the file added has changed, the cache should be invalidated for the ADD command.


Issue 1326 mentions other tips:

This worked.

RUN yum -y install firefox #redo

So it looks like Docker will re-run the step (and all the steps below it) if the string I am passing to RUN command changes in anyway - even it's just a comment.

The docker cache is used only, and only if none of his ancestor has changed (this behavior makes sense, as the next command will add change to the previous layer).

The cache is used if there isn't any character which has changed (so even a space is enough to invalidate a cache).


add && exit 0 after a command will invalidate the cache from there.

Example:

RUN apt-get install -y unzip && exit 0