Docker 会缓存已有镜像的镜像层,构建新镜像时,如果某镜像层已经存在,就直接使用,无需重新创建。
举例说明
1 root@ubuntu:~# cat Dockerfile 2 FROM ubuntu 3 RUN apt-get update && apt-get install -y vim 4 COPY testfile / 5 root@ubuntu:~# 6 root@ubuntu:~# echo test > testfile 7 root@ubuntu:~# 8 root@ubuntu:~# ls 9 Dockerfile testfile10 root@ubuntu:~# 11 root@ubuntu:~# docker build -t ubuntu-with-vi-dockerfile-2 .12 Sending build context to Docker daemon 22.53kB13 Step 1/3 : FROM ubuntu14 ---> 1d9c17228a9e15 Step 2/3 : RUN apt-get update && apt-get install -y vim16 ---> Using cache17 ---> 1e996ab06bb918 Step 3/3 : COPY testfile /19 ---> 06d809a4f0b320 Successfully built 06d809a4f0b321 Successfully tagged ubuntu-with-vi-dockerfile-2:latest22 root@ubuntu:~#
- 已有文件testfile
- 之前已经运行过相同的 RUN 指令,这次直接使用缓存镜像(1e996ab06bb9)执行 COPY 指令。
- 其过程是启动临时容器,复制 testfile,提交新的镜像层 06d809a4f0b3,删除临时容器。
- 生成镜像 ubuntu-with-vi-dockerfile-2:latest
Dockerfile 中每一个指令都会创建一个镜像层,上层是依赖于下层的。无论什么时候,只要某一层发生变化,其上面所有层的缓存都会失效。
交换前面 RUN 和 COPY 的顺序,缓存失效了,具体执行过程如下。
1 root@ubuntu:~# cat Dockerfile 2 FROM ubuntu 3 COPY testfile / 4 RUN apt-get update && apt-get install -y vim 5 root@ubuntu:~# ls 6 Dockerfile testfile 7 root@ubuntu:~# docker build -t ubuntu-with-vi-dockerfile-3 . 8 Sending build context to Docker daemon 22.53kB 9 Step 1/3 : FROM ubuntu 10 ---> 1d9c17228a9e 11 Step 2/3 : COPY testfile / 12 ---> 7d12b6a5ab50 13 Step 3/3 : RUN apt-get update && apt-get install -y vim 14 ---> Running in 046839fb7974 15 Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB] 16 Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB] 17 Get:3 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [135 kB] 18 Get:4 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [300 kB] 19 Get:5 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [1367 B] 20 Get:6 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB] 21 Get:7 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB] 22 Get:8 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB] 23 Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB] 24 Get:10 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB] 25 Get:11 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB] 26 Get:12 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [618 kB] 27 Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [10.7 kB] 28 Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [900 kB] 29 Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [6931 B] 30 Get:16 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [3655 B] 31 Fetched 15.3 MB in 1min 3s (243 kB/s) 32 Reading package lists... 33 Reading package lists... 34 Building dependency tree... 35 Reading state information... 36 The following additional packages will be installed: 37 file libexpat1 libgpm2 libmagic-mgc libmagic1 libmpdec2 libpython3.6 38 libpython3.6-minimal libpython3.6-stdlib libreadline7 libsqlite3-0 libssl1.1 39 mime-support readline-common vim-common vim-runtime xxd xz-utils 40 Suggested packages: 41 gpm readline-doc ctags vim-doc vim-scripts 42 The following NEW packages will be installed: 43 file libexpat1 libgpm2 libmagic-mgc libmagic1 libmpdec2 libpython3.6 44 libpython3.6-minimal libpython3.6-stdlib libreadline7 libsqlite3-0 libssl1.1 45 mime-support readline-common vim vim-common vim-runtime xxd xz-utils 46 0 upgraded, 19 newly installed, 0 to remove and 0 not upgraded. 47 Need to get 12.7 MB of archives. 48 After this operation, 61.0 MB of additional disk space will be used. 49 Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libmagic-mgc amd64 1:5.32-2ubuntu0.1 [184 kB] 50 Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libmagic1 amd64 1:5.32-2ubuntu0.1 [68.4 kB] 51 Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 file amd64 1:5.32-2ubuntu0.1 [22.1 kB] 52 Get:4 http://archive.ubuntu.com/ubuntu bionic/main amd64 libexpat1 amd64 2.2.5-3 [80.2 kB] 53 Get:5 http://archive.ubuntu.com/ubuntu bionic/main amd64 libmpdec2 amd64 2.4.2-1ubuntu1 [84.1 kB] 54 Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libssl1.1 amd64 1.1.0g-2ubuntu4.3 [1130 kB] 55 Get:7 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6-minimal amd64 3.6.7-1~18.04 [531 kB] 56 Get:8 http://archive.ubuntu.com/ubuntu bionic/main amd64 mime-support all 3.60ubuntu1 [30.1 kB] 57 Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 readline-common all 7.0-3 [52.9 kB] 58 Get:10 http://archive.ubuntu.com/ubuntu bionic/main amd64 libreadline7 amd64 7.0-3 [124 kB] 59 Get:11 http://archive.ubuntu.com/ubuntu bionic/main amd64 libsqlite3-0 amd64 3.22.0-1 [496 kB] 60 Get:12 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6-stdlib amd64 3.6.7-1~18.04 [1711 kB] 61 Get:13 http://archive.ubuntu.com/ubuntu bionic/main amd64 xxd amd64 2:8.0.1453-1ubuntu1 [49.2 kB] 62 Get:14 http://archive.ubuntu.com/ubuntu bionic/main amd64 vim-common all 2:8.0.1453-1ubuntu1 [70.4 kB] 63 Get:15 http://archive.ubuntu.com/ubuntu bionic/main amd64 xz-utils amd64 5.2.2-1.3 [83.8 kB] 64 Get:16 http://archive.ubuntu.com/ubuntu bionic/main amd64 libgpm2 amd64 1.20.7-5 [15.1 kB] 65 Get:17 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6 amd64 3.6.7-1~18.04 [1415 kB] 66 Get:18 http://archive.ubuntu.com/ubuntu bionic/main amd64 vim-runtime all 2:8.0.1453-1ubuntu1 [5437 kB] 67 Get:19 http://archive.ubuntu.com/ubuntu bionic/main amd64 vim amd64 2:8.0.1453-1ubuntu1 [1152 kB] 68 debconf: delaying package configuration, since apt-utils is not installed 69 Fetched 12.7 MB in 8s (1632 kB/s) 70 Selecting previously unselected package libmagic-mgc. 71 (Reading database ... 4039 files and directories currently installed.) 72 Preparing to unpack .../00-libmagic-mgc_1%3a5.32-2ubuntu0.1_amd64.deb ... 73 Unpacking libmagic-mgc (1:5.32-2ubuntu0.1) ... 74 Selecting previously unselected package libmagic1:amd64. 75 Preparing to unpack .../01-libmagic1_1%3a5.32-2ubuntu0.1_amd64.deb ... 76 Unpacking libmagic1:amd64 (1:5.32-2ubuntu0.1) ... 77 Selecting previously unselected package file. 78 Preparing to unpack .../02-file_1%3a5.32-2ubuntu0.1_amd64.deb ... 79 Unpacking file (1:5.32-2ubuntu0.1) ... 80 Selecting previously unselected package libexpat1:amd64. 81 Preparing to unpack .../03-libexpat1_2.2.5-3_amd64.deb ... 82 Unpacking libexpat1:amd64 (2.2.5-3) ... 83 Selecting previously unselected package libmpdec2:amd64. 84 Preparing to unpack .../04-libmpdec2_2.4.2-1ubuntu1_amd64.deb ... 85 Unpacking libmpdec2:amd64 (2.4.2-1ubuntu1) ... 86 Selecting previously unselected package libssl1.1:amd64. 87 Preparing to unpack .../05-libssl1.1_1.1.0g-2ubuntu4.3_amd64.deb ... 88 Unpacking libssl1.1:amd64 (1.1.0g-2ubuntu4.3) ... 89 Selecting previously unselected package libpython3.6-minimal:amd64. 90 Preparing to unpack .../06-libpython3.6-minimal_3.6.7-1~18.04_amd64.deb ... 91 Unpacking libpython3.6-minimal:amd64 (3.6.7-1~18.04) ... 92 Selecting previously unselected package mime-support. 93 Preparing to unpack .../07-mime-support_3.60ubuntu1_all.deb ... 94 Unpacking mime-support (3.60ubuntu1) ... 95 Selecting previously unselected package readline-common. 96 Preparing to unpack .../08-readline-common_7.0-3_all.deb ... 97 Unpacking readline-common (7.0-3) ... 98 Selecting previously unselected package libreadline7:amd64. 99 Preparing to unpack .../09-libreadline7_7.0-3_amd64.deb ...100 Unpacking libreadline7:amd64 (7.0-3) ...101 Selecting previously unselected package libsqlite3-0:amd64.102 Preparing to unpack .../10-libsqlite3-0_3.22.0-1_amd64.deb ...103 Unpacking libsqlite3-0:amd64 (3.22.0-1) ...104 Selecting previously unselected package libpython3.6-stdlib:amd64.105 Preparing to unpack .../11-libpython3.6-stdlib_3.6.7-1~18.04_amd64.deb ...106 Unpacking libpython3.6-stdlib:amd64 (3.6.7-1~18.04) ...107 Selecting previously unselected package xxd.108 Preparing to unpack .../12-xxd_2%3a8.0.1453-1ubuntu1_amd64.deb ...109 Unpacking xxd (2:8.0.1453-1ubuntu1) ...110 Selecting previously unselected package vim-common.111 Preparing to unpack .../13-vim-common_2%3a8.0.1453-1ubuntu1_all.deb ...112 Unpacking vim-common (2:8.0.1453-1ubuntu1) ...113 Selecting previously unselected package xz-utils.114 Preparing to unpack .../14-xz-utils_5.2.2-1.3_amd64.deb ...115 Unpacking xz-utils (5.2.2-1.3) ...116 Selecting previously unselected package libgpm2:amd64.117 Preparing to unpack .../15-libgpm2_1.20.7-5_amd64.deb ...118 Unpacking libgpm2:amd64 (1.20.7-5) ...119 Selecting previously unselected package libpython3.6:amd64.120 Preparing to unpack .../16-libpython3.6_3.6.7-1~18.04_amd64.deb ...121 Unpacking libpython3.6:amd64 (3.6.7-1~18.04) ...122 Selecting previously unselected package vim-runtime.123 Preparing to unpack .../17-vim-runtime_2%3a8.0.1453-1ubuntu1_all.deb ...124 Adding 'diversion of /usr/share/vim/vim80/doc/help.txt to /usr/share/vim/vim80/doc/help.txt.vim-tiny by vim-runtime'125 Adding 'diversion of /usr/share/vim/vim80/doc/tags to /usr/share/vim/vim80/doc/tags.vim-tiny by vim-runtime'126 Unpacking vim-runtime (2:8.0.1453-1ubuntu1) ...127 Selecting previously unselected package vim.128 Preparing to unpack .../18-vim_2%3a8.0.1453-1ubuntu1_amd64.deb ...129 Unpacking vim (2:8.0.1453-1ubuntu1) ...130 Setting up readline-common (7.0-3) ...131 Setting up libexpat1:amd64 (2.2.5-3) ...132 Setting up mime-support (3.60ubuntu1) ...133 Setting up xxd (2:8.0.1453-1ubuntu1) ...134 Setting up libgpm2:amd64 (1.20.7-5) ...135 Setting up libreadline7:amd64 (7.0-3) ...136 Setting up libmagic-mgc (1:5.32-2ubuntu0.1) ...137 Setting up libmagic1:amd64 (1:5.32-2ubuntu0.1) ...138 Processing triggers for libc-bin (2.27-3ubuntu1) ...139 Setting up libssl1.1:amd64 (1.1.0g-2ubuntu4.3) ...140 debconf: unable to initialize frontend: Dialog141 debconf: (TERM is not set, so the dialog frontend is not usable.)142 debconf: falling back to frontend: Readline143 debconf: unable to initialize frontend: Readline144 debconf: (Can't locate Term/ReadLine.pm in @INC (you may need to install the Term::ReadLine module) (@INC contains: /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.26.1 /usr/local/share/perl/5.26.1 /usr/lib/x86_64-linux-gnu/perl5/5.26 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.26 /usr/share/perl/5.26 /usr/local/lib/site_perl /usr/lib/x86_64-linux-gnu/perl-base) at /usr/share/perl5/Debconf/FrontEnd/Readline.pm line 7.)145 debconf: falling back to frontend: Teletype146 Setting up xz-utils (5.2.2-1.3) ...147 update-alternatives: using /usr/bin/xz to provide /usr/bin/lzma (lzma) in auto mode148 update-alternatives: warning: skip creation of /usr/share/man/man1/lzma.1.gz because associated file /usr/share/man/man1/xz.1.gz (of link group lzma) doesn't exist149 update-alternatives: warning: skip creation of /usr/share/man/man1/unlzma.1.gz because associated file /usr/share/man/man1/unxz.1.gz (of link group lzma) doesn't exist150 update-alternatives: warning: skip creation of /usr/share/man/man1/lzcat.1.gz because associated file /usr/share/man/man1/xzcat.1.gz (of link group lzma) doesn't exist151 update-alternatives: warning: skip creation of /usr/share/man/man1/lzmore.1.gz because associated file /usr/share/man/man1/xzmore.1.gz (of link group lzma) doesn't exist152 update-alternatives: warning: skip creation of /usr/share/man/man1/lzless.1.gz because associated file /usr/share/man/man1/xzless.1.gz (of link group lzma) doesn't exist153 update-alternatives: warning: skip creation of /usr/share/man/man1/lzdiff.1.gz because associated file /usr/share/man/man1/xzdiff.1.gz (of link group lzma) doesn't exist154 update-alternatives: warning: skip creation of /usr/share/man/man1/lzcmp.1.gz because associated file /usr/share/man/man1/xzcmp.1.gz (of link group lzma) doesn't exist155 update-alternatives: warning: skip creation of /usr/share/man/man1/lzgrep.1.gz because associated file /usr/share/man/man1/xzgrep.1.gz (of link group lzma) doesn't exist156 update-alternatives: warning: skip creation of /usr/share/man/man1/lzegrep.1.gz because associated file /usr/share/man/man1/xzegrep.1.gz (of link group lzma) doesn't exist157 update-alternatives: warning: skip creation of /usr/share/man/man1/lzfgrep.1.gz because associated file /usr/share/man/man1/xzfgrep.1.gz (of link group lzma) doesn't exist158 Setting up vim-common (2:8.0.1453-1ubuntu1) ...159 Setting up libsqlite3-0:amd64 (3.22.0-1) ...160 Setting up vim-runtime (2:8.0.1453-1ubuntu1) ...161 Setting up libmpdec2:amd64 (2.4.2-1ubuntu1) ...162 Setting up file (1:5.32-2ubuntu0.1) ...163 Setting up libpython3.6-minimal:amd64 (3.6.7-1~18.04) ...164 Setting up libpython3.6-stdlib:amd64 (3.6.7-1~18.04) ...165 Setting up libpython3.6:amd64 (3.6.7-1~18.04) ...166 Setting up vim (2:8.0.1453-1ubuntu1) ...167 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode168 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode169 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode170 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode171 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode172 update-alternatives: warning: skip creation of /usr/share/man/fr/man1/vi.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group vi) doesn't exist173 update-alternatives: warning: skip creation of /usr/share/man/it/man1/vi.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group vi) doesn't exist174 update-alternatives: warning: skip creation of /usr/share/man/pl/man1/vi.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group vi) doesn't exist175 update-alternatives: warning: skip creation of /usr/share/man/ru/man1/vi.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group vi) doesn't exist176 update-alternatives: warning: skip creation of /usr/share/man/ja/man1/vi.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group vi) doesn't exist177 update-alternatives: warning: skip creation of /usr/share/man/man1/vi.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group vi) doesn't exist178 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode179 update-alternatives: warning: skip creation of /usr/share/man/fr/man1/view.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group view) doesn't exist180 update-alternatives: warning: skip creation of /usr/share/man/it/man1/view.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group view) doesn't exist181 update-alternatives: warning: skip creation of /usr/share/man/pl/man1/view.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group view) doesn't exist182 update-alternatives: warning: skip creation of /usr/share/man/ru/man1/view.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group view) doesn't exist183 update-alternatives: warning: skip creation of /usr/share/man/ja/man1/view.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group view) doesn't exist184 update-alternatives: warning: skip creation of /usr/share/man/man1/view.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group view) doesn't exist185 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode186 update-alternatives: warning: skip creation of /usr/share/man/fr/man1/ex.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group ex) doesn't exist187 update-alternatives: warning: skip creation of /usr/share/man/it/man1/ex.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group ex) doesn't exist188 update-alternatives: warning: skip creation of /usr/share/man/pl/man1/ex.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group ex) doesn't exist189 update-alternatives: warning: skip creation of /usr/share/man/ru/man1/ex.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group ex) doesn't exist190 update-alternatives: warning: skip creation of /usr/share/man/ja/man1/ex.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group ex) doesn't exist191 update-alternatives: warning: skip creation of /usr/share/man/man1/ex.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group ex) doesn't exist192 update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode193 update-alternatives: warning: skip creation of /usr/share/man/fr/man1/editor.1.gz because associated file /usr/share/man/fr/man1/vim.1.gz (of link group editor) doesn't exist194 update-alternatives: warning: skip creation of /usr/share/man/it/man1/editor.1.gz because associated file /usr/share/man/it/man1/vim.1.gz (of link group editor) doesn't exist195 update-alternatives: warning: skip creation of /usr/share/man/pl/man1/editor.1.gz because associated file /usr/share/man/pl/man1/vim.1.gz (of link group editor) doesn't exist196 update-alternatives: warning: skip creation of /usr/share/man/ru/man1/editor.1.gz because associated file /usr/share/man/ru/man1/vim.1.gz (of link group editor) doesn't exist197 update-alternatives: warning: skip creation of /usr/share/man/ja/man1/editor.1.gz because associated file /usr/share/man/ja/man1/vim.1.gz (of link group editor) doesn't exist198 update-alternatives: warning: skip creation of /usr/share/man/man1/editor.1.gz because associated file /usr/share/man/man1/vim.1.gz (of link group editor) doesn't exist199 Processing triggers for libc-bin (2.27-3ubuntu1) ...200 Removing intermediate container 046839fb7974201 ---> 50d0892af06e202 Successfully built 50d0892af06e203 Successfully tagged ubuntu-with-vi-dockerfile-3:latest
------------引用来自--------------
https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587602&idx=1&sn=a473c628a69c12722fdbccbb95c35b94&chksm=8d30808bba47099dc433d15793803efaae66dd4ce885a608b725a827c9034a5dbcff65200662&scene=21#wechat_redirect