Get the App
SLTechnology News&Howtos  ›  Servers  › 

What are the caching characteristics of Docker images?

Shulou Source: shulou.com Published: 2022-06-01 00:49:39 09月14日 Update

This article will explain in detail how the caching characteristics of Docker images are. The content of the article is of high quality. Therefore, Xiaobian shares it with you as a reference. I hope that after reading this article, you will have a certain understanding of relevant knowledge.

Docker caches the image layers of existing images. When building a new image, if an image layer already exists, it will be used directly without creating it again.

Give an example.

Add something new to the Dockerfile above and copy a file into the mirror:

root@ubuntu:~# ls ①

Dockerfile testfile

root@ubuntu:~#

root@ubuntu:~# docker build -t ubuntu-with-vi-dockerfile-2 .

Sending build context to Docker daemon 32.77 kB

Step 1 : FROM ubuntu

---> f753707788c5

Step 2 : RUN apt-get update && apt-get install -y vim

---> Using cache ②

---> 35ca89798937

Step 3 : COPY testfile / ③

---> 8d02784a78f4

Removing intermediate container bf2b4040f4e9

Successfully built 8d02784a78f4

Make sure the testfile exists.

② The key point is here: the same RUN instruction has been run before, this time directly using the mirror layer 35ca89798937 in the cache.

3. Execute COPY command.

The procedure is to start the temporary container, copy testfile, commit the new mirror layer 8d 02784a78 f4, and delete the temporary container.

Add a layer directly to the ubuntu-with-vi-dockerfile image and you get the new image ubuntu-with-vi-dockerfile-2.

If we want to build images without caching, we can add the--no-cache parameter to the docker build command.

Each instruction in Dockerfile creates a mirror layer, with the upper layer dependent on the lower layer. Whenever a layer changes, all the caches above it are invalidated.

That is, if we change the order in which Dockerfile instructions are executed, or modify or add instructions, the cache will be invalidated.

For example, swap the order of RUN and COPY before.

Although logically this change has no effect on the contents of the image, Docker must rebuild the affected image layers due to the layered nature of the structure.

root@ubuntu:~# docker build -t ubuntu-with-vi-dockerfile-3 .

Sending build context to Docker daemon 37.89 kB

Step 1 : FROM ubuntu

---> f753707788c5

Step 2 : COPY testfile /

---> bc87c9710f40

Removing intermediate container 04ff324d6af5

Step 3 : RUN apt-get update && apt-get install -y vim

---> Running in 7f0fcb5ee373

Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]

......

From the output above you can see that the new mirror layer bc87c9710f40 has been generated and the cache has been invalidated.

In addition to using cache when building, Docker also uses it when downloading images. For example, we download httpd images.

The docker pull command output shows that the first layer (base image) already exists and does not need to be downloaded.

Dockerfile shows that the base image of httpd is debian, which happens to have been downloaded before, so there is cache available. Docker history can be used for further verification.

About Docker image caching features is how to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Tags: Mirroring caching instructions features content commands containers articles more knowledge articles order plus one influence output yes same upper layer that is that is to say Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Linux Shulou Tech Info MariaDB