Docker Build Cache: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:
=Overview=
=Overview=
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]].
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 [[Docker Build Cache#Overview|build cache]], looking for cached layers that can be reused, instead of building a duplicate image from scratch.  
As the Dockerfile instructions are evaluated in order, Docker inspects the [[Docker Build Cache#Overview|build cache]], looking for cached layers that can be reused, instead of building a duplicate image from scratch.  
Line 16: Line 18:
* 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]].
* In case of [[Dockerfile#ADD|ADD]], [[Dockerfile#COPY|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.
* In case of [[Dockerfile#ADD|ADD]], [[Dockerfile#COPY|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 [[Dockerfile#ADD|ADD]] and [[Dockerfile#COPY|COPY]] commands, cache checking does not look at the file arguments to determine a cache match. <tt>RUN yum -y update</tt> or <tt>RUN apt-get -y update</tt> commands '''require''' [[Docker_build#--no-cache|--no-cache=true]] to be effective.
* Aside from [[Dockerfile#ADD|ADD]] and [[Dockerfile#COPY|COPY]] commands, cache checking does not look at the file arguments to determine a cache match. The following commands '''require''' [[Docker_build#--no-cache|--no-cache=true]] to be effective:
** <tt>RUN yum -y update</tt>
** <tt>RUN apt-get -y update</tt>
** <tt>git clone</tt>
* Once the cache is invalidated, all subsequent Dockerfile commands generate new images and the cache is not used.
* Once the cache is invalidated, all subsequent Dockerfile commands generate new images and the cache is not used.



Revision as of 21:40, 21 October 2020

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