Docker Build Cache

From NovaOrdis Knowledge Base
Revision as of 21:40, 21 October 2020 by Ovidiu (talk | contribs) (→‎Overview)
Jump to navigation Jump to search

External

Internal

Overview

The Docker build cache is used during the image build process.

If the Dockerfile does not change, and if none of its instructions are ADD or COPY, the corresponding cached image will be used. If a line in Docker file had changed, the build process will rebuild the layers from that line onward.

As the Dockerfile instructions are evaluated in order, Docker inspects the build cache, looking for cached layers that can be reused, instead of building a duplicate image from scratch.

The commands are matched against cached images, according to the following rules:

  • Starting with a parent image that is already in the cache, the next instruction is compared against all cached child images derived from that parent image, to see if one of them was built using the exact same instruction. If no such child image exists, the cache is invalidated.
  • The literal instruction is used in comparison (presumably the cache maintains those instructions), with the exception of ADD and COPY.
  • In case of ADD, COPY, the contents of the files to be copied are examined and a checksum is calculated for each file. The last-modified and last-accessed times of the file(s) are not considered in these checksums. The corresponding files in cache are also checksummed and the checksums are compared. If anything changed in the file - content or metadata - the cache is invalidated.
  • Aside from ADD and COPY commands, cache checking does not look at the file arguments to determine a cache match. The following commands require --no-cache=true to be effective:
    • RUN yum -y update
    • RUN apt-get -y update
    • git clone
  • Once the cache is invalidated, all subsequent Dockerfile commands generate new images and the cache is not used.

The build cache can be explicitly invalidated if the docker build --no-cache=true command line option is used.

If a cached image was used, the build command output states that:

Step 2/5 : COPY ./loop /opt/loop
 ---> Using cache
 ---> 171da11cf0ef