Remove Docker’s Cached Intermediate Images

Docker’s caching of RUN results into images helps speed up builds of Dockerfiles, but developers need to remove some cached images during development. There’s no easy way to do this, but some workarounds have been recommended.

Docker Caches the Result of a RUN

Whenever docker process a RUN command, it creates a new, intermediate image. If the Dockerfile is run again, and a RUN command is unchanged, it goes into the cache and retrieves the image.

The problem: if there’s a file that’s copied into the container, and the contents of the file change, it doesn’t cause Docker to invalidate the cached copy. For example, you have these lines:

ADD postfix.sh /opt/postfix.sh
RUN chmod +x /opt/postfix.sh

(OK, that’s bad form, I know). The RUN line isn’t executed and the result cached when the contents of postfix.sh change.

–no-cache is Slow

The quick fix is to build with the –no-cache option, but that is super slow, especially if you’re downloading packages.

The recommended fixes are in two articles I found:

Not too Proud to Fake It

I found a lazier, simpler solution: put some noise into the RUN line.

RUN touch /tmp/123 && chmod +x /opt/postfix.sh

touch /tmp/123 doesn’t do anything. It’s just there so you can change the filename and force Docker to re-run the line and cache the result into an image.

This doesn’t remove the image files, but you can take care of that later, once the container is working.