1. 程式人生 > 實用技巧 >docker映象之快取特性

docker映象之快取特性

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) <[email protected]>"
 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