Docker Build Cache: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
No edit summary
Line 9: Line 9:
The Docker build cache is used during the [[Docker_build#The_Build_Process|image build process]].
The Docker build cache is used during the [[Docker_build#The_Build_Process|image build process]].


As the Dockerfile instructions are evaluated in order, Docker inspects the [[Docker Build Cache#Overview|build cache]], looking for cached ''images'' that can be reused, instead of building a duplicate image from scratch. In matching commands to cached images, the following rules are followed:
As the Dockerfile instructions are evaluated in order, Docker inspects the [[Docker Build Cache#Overview|build cache]], looking for cached ''images'' that can be reused, instead of building a duplicate image from scratch.  
 
The commands are matched against cached layers, 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.
* 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 [[Dockerfile#ADD|ADD]] and [[Dockerfile#COPY|COPY]].
* The literal instruction is used in comparison (presumably the cache maintains those instructions), with the exception of [[Dockerfile#ADD|ADD]] and [[Dockerfile#COPY|COPY]].

Revision as of 21:33, 21 October 2020

External

Internal

Overview

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

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

The commands are matched against cached layers, 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. RUN yum -y update or RUN apt-get -y update commands require --no-cache=true to be effective.
  • 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