1、docker镜像的缓存特性是怎样的?如何说明?

Docker 会缓存已有镜像的镜像层,构建新镜像时,如果某镜像层已经存在,就直接使用,无需重新创建。

root@richardo-docker01:~# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
centos-with-vim-dockerfile-2   latest              fa86e8214b98        2 days ago          272MB
centos-with-vim-dockerfile     latest              e99684eeca8a        2 days ago          272MB
centos-with-vim                latest              0ec55bc57a3d        2 days ago          272MB
httpd                          latest              3dd970e6b110        4 weeks ago         138MB
centos                         latest              0d120b6ccaa8        3 months ago        215MB
hello-world                    latest              bf756fb1ae65        10 months ago       13.3kB
root@richardo-docker01:~# ls
0d120b6ccaa8  Dockerfile  Running
root@richardo-docker01:~# touch testfile
root@richardo-docker01:~# vim Dockerfile
root@richardo-docker01:~# docker build -t centos-with-vim-dockerfile-2 .
Sending build context to Docker daemon  14.85kB
Step 1/3 : FROM centos
 ---> 0d120b6ccaa8
Step 2/3 : RUN yum makecache && yum install -y vim
 ---> Using cache
 ---> e99684eeca8a
Step 3/3 : COPY testfile /
 ---> fa86e8214b98
Successfully built fa86e8214b98
Successfully tagged centos-with-vim-dockerfile-2:latest
root@richardo-docker01:~#

2、如果希望构建镜像的时候不使用缓存?应该怎么做?

如果我们希望在构建镜像时不使用缓存,可以在 docker build 命令中加上 --no-cache 参数。

3、什么情况下绑在会失效?

Dockerfile 中每一个指令都会创建一个镜像层,上层是依赖于下层的。无论什么时候,只要某一层发生变化,其上面所有层的缓存都会失效。
也就是说,如果我们改变 Dockerfile 指令的执行顺序,或者修改或添加指令,都会使缓存失效。
eg:交换前面 RUN 和 COPY 的顺序
虽然在逻辑上这种改动对镜像的内容没有影响,但由于分层的结构特性,Docker 必须重建受影响的镜像层。

root@richardo-docker01:~# cat Dockerfile
FROM centos
COPY testfile /
RUN yum makecache && yum install -y vim
root@richardo-docker01:~# docker build -t centos-with-vim-dockerfile-3 .
Sending build context to Docker daemon  15.36kB
Step 1/3 : FROM centos
 ---> 0d120b6ccaa8
Step 2/3 : COPY testfile /
 ---> 32568ab9551d
Step 3/3 : RUN yum makecache && yum install -y vim
 ---> Running in 3d10325475a8
CentOS-8 - AppStream                            1.8 MB/s | 5.8 MB     00:03
CentOS-8 - Base                                 1.9 MB/s | 2.2 MB     00:01
CentOS-8 - Extras                                13 kB/s | 8.6 kB     00:00
Last metadata expiration check: 0:00:01 ago on Mon Nov 16 01:26:25 2020.
Metadata cache created.
Last metadata expiration check: 0:00:04 ago on Mon Nov 16 01:26:25 2020.
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-13.el8         AppStream      1.4 M
Installing dependencies:
 gpm-libs            x86_64      1.20.7-15.el8             AppStream       39 k
 vim-common          x86_64      2:8.0.1763-13.el8         AppStream      6.3 M
 vim-filesystem      noarch      2:8.0.1763-13.el8         AppStream       48 k
 which               x86_64      2.21-12.el8               BaseOS          49 k

Transaction Summary
================================================================================
Install  5 Packages

Total download size: 7.8 M
Installed size: 31 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm        238 kB/s |  39 kB     00:00
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 616 kB/s |  48 kB     00:00
(3/5): which-2.21-12.el8.x86_64.rpm             433 kB/s |  49 kB     00:00
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm  1.5 MB/s | 1.4 MB     00:00
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm    1.7 MB/s | 6.3 MB     00:03
--------------------------------------------------------------------------------
Total                                           1.4 MB/s | 7.8 MB     00:05
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream                             66 kB/s | 1.6 kB     00:00
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : which-2.21-12.el8.x86_64                               1/5
  Installing       : vim-filesystem-2:8.0.1763-13.el8.noarch                2/5
  Installing       : vim-common-2:8.0.1763-13.el8.x86_64                    3/5
  Installing       : gpm-libs-1.20.7-15.el8.x86_64                          4/5
  Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64                          4/5
  Installing       : vim-enhanced-2:8.0.1763-13.el8.x86_64                  5/5
  Running scriptlet: vim-enhanced-2:8.0.1763-13.el8.x86_64                  5/5
  Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64                    5/5
  Verifying        : gpm-libs-1.20.7-15.el8.x86_64                          1/5
  Verifying        : vim-common-2:8.0.1763-13.el8.x86_64                    2/5
  Verifying        : vim-enhanced-2:8.0.1763-13.el8.x86_64                  3/5
  Verifying        : vim-filesystem-2:8.0.1763-13.el8.noarch                4/5
  Verifying        : which-2.21-12.el8.x86_64                               5/5

Installed:
  gpm-libs-1.20.7-15.el8.x86_64         vim-common-2:8.0.1763-13.el8.x86_64
  vim-enhanced-2:8.0.1763-13.el8.x86_64 vim-filesystem-2:8.0.1763-13.el8.noarch
  which-2.21-12.el8.x86_64

Complete!
Removing intermediate container 3d10325475a8
 ---> f41e704ce73b
Successfully built f41e704ce73b
Successfully tagged centos-with-vim-dockerfile-3:latest

4、除了构建的时候用缓存还有什么时候会用缓存?

除了构建时使用缓存,Docker 在下载镜像时也会使用。例如我们下载 httpd 镜像。
由 Dockerfile 可知 httpd 的 base 镜像为 debian,正好之前已经下载过 debian 镜像,所以有缓存可用。通过 docker history 可以进一步验证。
https://mp.weixin.qq.com/s/aEi_-nm49EuFBecae4-Baw