第 3 章 镜像 - 013 - Dockerfile 构建镜像

第一个 Dockerfile

 FROM ubuntu
RUN apt-get update && apt-get install -y vim

运行 docker build 命令构建镜像并详细分析每个细节

 root@ubuntu:~# pwd
/root
root@ubuntu:~#
root@ubuntu:~# cat Dockerfile
FROM ubuntu
RUN apt-get update && apt-get install -y vim
root@ubuntu:~#
root@ubuntu:~# docker build -t ubuntu-with-vi-dockerfile .
Sending build context to Docker daemon .06kB
Step / : FROM ubuntu
---> 1d9c17228a9e
Step / : RUN apt-get update && apt-get install -y vim
---> Running in 1ffb347c507f
Get: http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
Get: http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get: http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [1367 B]
Get: http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [135 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get: http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [300 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get: http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [10.7 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [6931 B]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [900 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [618 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [3655 B]
Fetched 15.3 MB in 48s ( kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
file libexpat1 libgpm2 libmagic-mgc libmagic1 libmpdec2 libpython3.
libpython3.-minimal libpython3.-stdlib libreadline7 libsqlite3- libssl1.
mime-support readline-common vim-common vim-runtime xxd xz-utils
Suggested packages:
gpm readline-doc ctags vim-doc vim-scripts
The following NEW packages will be installed:
file libexpat1 libgpm2 libmagic-mgc libmagic1 libmpdec2 libpython3.
libpython3.-minimal libpython3.-stdlib libreadline7 libsqlite3- libssl1.
mime-support readline-common vim vim-common vim-runtime xxd xz-utils
upgraded, newly installed, to remove and not upgraded.
Need to get 12.7 MB of archives.
After this operation, 61.0 MB of additional disk space will be used.
Get: http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libmagic-mgc amd64 1:5.32-2ubuntu0.1 [184 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libmagic1 amd64 1:5.32-2ubuntu0.1 [68.4 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 file amd64 1:5.32-2ubuntu0.1 [22.1 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 libexpat1 amd64 2.2.5-3 [80.2 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 libmpdec2 amd64 2.4.2-1ubuntu1 [84.1 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libssl1.1 amd64 1.1.0g-2ubuntu4.3 [1130 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6-minimal amd64 3.6.7-1~18.04 [531 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 mime-support all 3.60ubuntu1 [30.1 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 readline-common all 7.0-3 [52.9 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 libreadline7 amd64 7.0-3 [124 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 libsqlite3-0 amd64 3.22.0-1 [496 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6-stdlib amd64 3.6.7-1~18.04 [1711 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 xxd amd64 2:8.0.1453-1ubuntu1 [49.2 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 vim-common all 2:8.0.1453-1ubuntu1 [70.4 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 xz-utils amd64 5.2.2-1.3 [83.8 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 libgpm2 amd64 1.20.7-5 [15.1 kB]
Get: http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6 amd64 3.6.7-1~18.04 [1415 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 vim-runtime all 2:8.0.1453-1ubuntu1 [5437 kB]
Get: http://archive.ubuntu.com/ubuntu bionic/main amd64 vim amd64 2:8.0.1453-1ubuntu1 [1152 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 12.7 MB in 38s ( kB/s)
Selecting previously unselected package libmagic-mgc.
(Reading database ... files and directories currently installed.)
Preparing to unpack .../-libmagic-mgc_1%3a5.-2ubuntu0.1_amd64.deb ...
Unpacking libmagic-mgc (:5.32-2ubuntu0.) ...
Selecting previously unselected package libmagic1:amd64.
Preparing to unpack .../-libmagic1_1%3a5.-2ubuntu0.1_amd64.deb ...
Unpacking libmagic1:amd64 (:5.32-2ubuntu0.) ...
Selecting previously unselected package file.
Preparing to unpack .../-file_1%3a5.-2ubuntu0.1_amd64.deb ...
Unpacking file (:5.32-2ubuntu0.) ...
Selecting previously unselected package libexpat1:amd64.
Preparing to unpack .../-libexpat1_2.2.5-3_amd64.deb ...
Unpacking libexpat1:amd64 (2.2.-) ...
Selecting previously unselected package libmpdec2:amd64.
Preparing to unpack .../-libmpdec2_2.4.2-1ubuntu1_amd64.deb ...
Unpacking libmpdec2:amd64 (2.4.-1ubuntu1) ...
Selecting previously unselected package libssl1.:amd64.
Preparing to unpack .../-libssl1.1_1..0g-2ubuntu4.3_amd64.deb ...
Unpacking libssl1.:amd64 (1.1.0g-2ubuntu4.) ...
Selecting previously unselected package libpython3.-minimal:amd64.
Preparing to unpack .../-libpython3.-minimal_3.6.7-~.04_amd64.deb ...
Unpacking libpython3.-minimal:amd64 (3.6.-~18.04) ...
Selecting previously unselected package mime-support.
Preparing to unpack .../-mime-support_3.60ubuntu1_all.deb ...
Unpacking mime-support (.60ubuntu1) ...
Selecting previously unselected package readline-common.
Preparing to unpack .../-readline-common_7.-3_all.deb ...
Unpacking readline-common (7.0-) ...
Selecting previously unselected package libreadline7:amd64.
Preparing to unpack .../-libreadline7_7.-3_amd64.deb ...
Unpacking libreadline7:amd64 (7.0-) ...
Selecting previously unselected package libsqlite3-:amd64.
Preparing to unpack .../-libsqlite3-0_3.22.0-1_amd64.deb ...
Unpacking libsqlite3-:amd64 (3.22.-) ...
Selecting previously unselected package libpython3.-stdlib:amd64.
Preparing to unpack .../-libpython3.-stdlib_3.6.7-~.04_amd64.deb ...
Unpacking libpython3.-stdlib:amd64 (3.6.-~18.04) ...
Selecting previously unselected package xxd.
Preparing to unpack .../-xxd_2%3a8.0.1453-1ubuntu1_amd64.deb ...
Unpacking xxd (:8.0.-1ubuntu1) ...
Selecting previously unselected package vim-common.
Preparing to unpack .../-vim-common_2%3a8.0.1453-1ubuntu1_all.deb ...
Unpacking vim-common (:8.0.-1ubuntu1) ...
Selecting previously unselected package xz-utils.
Preparing to unpack .../-xz-utils_5.2.2-.3_amd64.deb ...
Unpacking xz-utils (5.2.-1.3) ...
Selecting previously unselected package libgpm2:amd64.
Preparing to unpack .../-libgpm2_1.20.7-5_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.-) ...
Selecting previously unselected package libpython3.:amd64.
Preparing to unpack .../-libpython3.6_3.6.7-~.04_amd64.deb ...
Unpacking libpython3.:amd64 (3.6.-~18.04) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../-vim-runtime_2%3a8.0.1453-1ubuntu1_all.deb ...
Adding 'diversion of /usr/share/vim/vim80/doc/help.txt to /usr/share/vim/vim80/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim80/doc/tags to /usr/share/vim/vim80/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (:8.0.-1ubuntu1) ...
Selecting previously unselected package vim.
Preparing to unpack .../-vim_2%3a8.0.1453-1ubuntu1_amd64.deb ...
Unpacking vim (:8.0.-1ubuntu1) ...
Setting up readline-common (7.0-) ...
Setting up libexpat1:amd64 (2.2.-) ...
Setting up mime-support (.60ubuntu1) ...
Setting up xxd (:8.0.-1ubuntu1) ...
Setting up libgpm2:amd64 (1.20.-) ...
Setting up libreadline7:amd64 (7.0-) ...
Setting up libmagic-mgc (:5.32-2ubuntu0.) ...
Setting up libmagic1:amd64 (:5.32-2ubuntu0.) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Setting up libssl1.:amd64 (1.1.0g-2ubuntu4.) ...
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
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.)
debconf: falling back to frontend: Teletype
Setting up xz-utils (5.2.-1.3) ...
update-alternatives: using /usr/bin/xz to provide /usr/bin/lzma (lzma) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/man1/lzma..gz because associated file /usr/share/man/man1/xz..gz (of link group lzma) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/unlzma..gz because associated file /usr/share/man/man1/unxz..gz (of link group lzma) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/lzcat..gz because associated file /usr/share/man/man1/xzcat..gz (of link group lzma) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/lzmore..gz because associated file /usr/share/man/man1/xzmore..gz (of link group lzma) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/lzless..gz because associated file /usr/share/man/man1/xzless..gz (of link group lzma) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/lzdiff..gz because associated file /usr/share/man/man1/xzdiff..gz (of link group lzma) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/lzcmp..gz because associated file /usr/share/man/man1/xzcmp..gz (of link group lzma) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/lzgrep..gz because associated file /usr/share/man/man1/xzgrep..gz (of link group lzma) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/lzegrep..gz because associated file /usr/share/man/man1/xzegrep..gz (of link group lzma) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/lzfgrep..gz because associated file /usr/share/man/man1/xzfgrep..gz (of link group lzma) doesn't exist
Setting up vim-common (:8.0.-1ubuntu1) ...
Setting up libsqlite3-:amd64 (3.22.-) ...
Setting up vim-runtime (:8.0.-1ubuntu1) ...
Setting up libmpdec2:amd64 (2.4.-1ubuntu1) ...
Setting up file (:5.32-2ubuntu0.) ...
Setting up libpython3.-minimal:amd64 (3.6.-~18.04) ...
Setting up libpython3.-stdlib:amd64 (3.6.-~18.04) ...
Setting up libpython3.:amd64 (3.6.-~18.04) ...
Setting up vim (:8.0.-1ubuntu1) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/fr/man1/vi..gz because associated file /usr/share/man/fr/man1/vim..gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/it/man1/vi..gz because associated file /usr/share/man/it/man1/vim..gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/pl/man1/vi..gz because associated file /usr/share/man/pl/man1/vim..gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ru/man1/vi..gz because associated file /usr/share/man/ru/man1/vim..gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ja/man1/vi..gz because associated file /usr/share/man/ja/man1/vim..gz (of link group vi) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/vi..gz because associated file /usr/share/man/man1/vim..gz (of link group vi) doesn't exist
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/fr/man1/view..gz because associated file /usr/share/man/fr/man1/vim..gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/it/man1/view..gz because associated file /usr/share/man/it/man1/vim..gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/pl/man1/view..gz because associated file /usr/share/man/pl/man1/vim..gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ru/man1/view..gz because associated file /usr/share/man/ru/man1/vim..gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ja/man1/view..gz because associated file /usr/share/man/ja/man1/vim..gz (of link group view) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/view..gz because associated file /usr/share/man/man1/vim..gz (of link group view) doesn't exist
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/fr/man1/ex..gz because associated file /usr/share/man/fr/man1/vim..gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/it/man1/ex..gz because associated file /usr/share/man/it/man1/vim..gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/pl/man1/ex..gz because associated file /usr/share/man/pl/man1/vim..gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ru/man1/ex..gz because associated file /usr/share/man/ru/man1/vim..gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ja/man1/ex..gz because associated file /usr/share/man/ja/man1/vim..gz (of link group ex) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/ex..gz because associated file /usr/share/man/man1/vim..gz (of link group ex) doesn't exist
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
update-alternatives: warning: skip creation of /usr/share/man/fr/man1/editor..gz because associated file /usr/share/man/fr/man1/vim..gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/it/man1/editor..gz because associated file /usr/share/man/it/man1/vim..gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/pl/man1/editor..gz because associated file /usr/share/man/pl/man1/vim..gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ru/man1/editor..gz because associated file /usr/share/man/ru/man1/vim..gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/ja/man1/editor..gz because associated file /usr/share/man/ja/man1/vim..gz (of link group editor) doesn't exist
update-alternatives: warning: skip creation of /usr/share/man/man1/editor..gz because associated file /usr/share/man/man1/vim..gz (of link group editor) doesn't exist
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Removing intermediate container 1ffb347c507f
---> 1e996ab06bb9
Successfully built 1e996ab06bb9
Successfully tagged ubuntu-with-vi-dockerfile:latest
root@ubuntu:~#
root@ubuntu:~# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-with-vi-dockerfile latest 1e996ab06bb9 minutes ago 170MB
ubuntu-with-vi latest b5410dfa9a42 days ago 170MB
ubuntu latest 1d9c17228a9e days ago .7MB
root@ubuntu:~#
  1. 当前目录为 /root
  2. 准备Dockerfile
  3. 运行 docker build 命令,-t 指定新镜像名ubuntu-with-vi-dockerfile,命令末尾的 . 指明 build context 为当前目录。Docker 默认会从 build context 中查找 Dockerfile 文件,我们也可以通过 -f 参数指定 Dockerfile 的位置。
  4. 镜像真正的构建过程,Docker 将 build context 中的所有文件发送给 Docker daemon。build context 为镜像构建提供所需要的文件或目录。 Dockerfile 中的 ADD、COPY 等命令可以将 build context 中的文件添加到镜像。注:build context 为当前目录 /root,该目录下的所有文件和子目录都会被发送给 Docker daemon。所以不要将多余文件放到 build context,特别不要把 //usr 作为 build context,否则构建过程会相当缓慢甚至失败。
  5. Step 1:执行 FROM,将 ubuntu 作为 base 镜像。
  6. Step 2:执行 RUN,安装 vim。
  7. 启动临时容器(1ffb347c507f),在容器中通过 apt-get 安装 vim。
  8. 安装成功后,将容器保存为镜像。这一步底层使用的是类似 docker commit 的命令。
  9. 删除临时容器 9f4d4166f7e3。
  10. 镜像构建成功。 通过 docker images 查看镜像信息

镜像的构建历史

docker history 会显示镜像的构建历史,也就是 Dockerfile 的执行过程。

 root@ubuntu:~# docker history ubuntu
IMAGE CREATED CREATED BY SIZE COMMENT
1d9c17228a9e days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> days ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B
<missing> 5 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B
<missing> 5 days ago /bin/sh -c #(nop) ADD file:c0f17c7189fc11b6a… 86.7MB
root@ubuntu:~# docker history ubuntu-with-vi-dockerfile:latest
IMAGE CREATED CREATED BY SIZE COMMENT
1e996ab06bb9 25 minutes ago /bin/sh -c apt-get update && apt-get install… 83.8MB
1d9c17228a9e 5 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 5 days ago /bin/sh -c mkdir -p /run/systemd && echo 'do… 7B
<missing> 5 days ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0B
<missing> 5 days ago /bin/sh -c set -xe && echo '#!/bin/sh' > /… 745B
<missing> 5 days ago /bin/sh -c #(nop) ADD file:c0f17c7189fc11b6a… 86.7MB
root@ubuntu:~#

这个新镜像层的内容由 RUN apt-get update && apt-get install -y vim 生成。

------------引用来自------------

https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587598&idx=1&sn=d0952497b3a2272eee05cfb1e4e05e62&chksm=8d308097ba47098189b45ba63c4040cf94d687fb19c54b9cab593df9995612ce0b0632e9a54e&scene=21#wechat_redirect

上一篇:cloud-init 工作原理 - 每天5分钟玩转 OpenStack(171)


下一篇:线段树合并 || 树状数组 || 离散化 || BZOJ 4756: [Usaco2017 Jan]Promotion Counting || Luogu P3605 [USACO17JAN]Promotion Counting晋升者计数