Docker Build Cache: Difference between revisions
Jump to navigation
Jump to search
Line 10: | Line 10: | ||
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 | 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. | ||
The commands are matched against cached images, according to the following rules: | The commands are matched against cached images, according to the following rules: |
Revision as of 21:37, 21 October 2020
External
- https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#leverage-build-cache
- https://www.baeldung.com/linux/docker-build-cache
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 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. 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