『现学现忘』Docker基础 — 35、实战:自定义CentOS镜像
1)目标:自定义镜像
wokong_centos
。2)所用到的保留字指令:
FROM
:基础镜像,当前新镜像是基于哪个镜像的。MAINTAINER
:镜像维护者的姓名和邮箱地址。RUN
:容器构建时需要运行的命令。EXPOSE
:当前容器对外暴露出的端口(端口映射)。WORKDIR
:指定在创建容器后,终端默认登陆进来的工作目录,一个落脚点,也就是pwd
的位置。ENV
:用来在构建镜像过程中设置环境变量。CMD
:指定一个容器启动时要运行的命令。3)新Docker命令:
docker history
,查看镜像的变更历史。
1、前提说明
我们运行官方的CentOS镜像。
# 1.查看本地Docker镜像
[root@192 home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 300e315adb2f 3 months ago 209MB
# 2.查看正确运行的Docker容器,此时没有容器正在运行。
[root@192 home]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
# 3.启动centos镜像
[root@192 home]# docker run -it centos
[root@ed92adeebf72 /]#
# 4.在容器中执行如下操作
# 4.1 查看容器当前工作目录,在根目录
[root@ed92adeebf72 /]# pwd
/
# 4.2 使用vim命令编辑文件,发现没有vim命令
[root@ed92adeebf72 /]# vim abc.txt
bash: vim: command not found
# 4.3 查看Docker容器的IP地址,发现无法使用ifconfig命令。
[root@ed92adeebf72 /]# ifconfig
bash: ifconfig: command not found
出现以上问题是因为,CentOS方镜像为了保证镜像的大小,是使用最小启动系统的精简版Linux系统,其中会有很多命令没有。
所以,自定义wokong_centos
镜像的目的,使我们自己的镜像具备如下:
- 登陆后的默认路径为
/tmp
。 - 可以使用
vim
编辑器。 - 支持查看网络配置
ifconfig
命令。
2、编写Dockerfile文件
在宿主机的home
目录中创建mydockerfile
文件夹,用于存放我们编写的Dockerfile文件。
# 创建mydockerfile文件夹
[root@192 home]# ll
总用量 0
[root@192 home]# mkdir mydockerfile
[root@192 home]# ll
总用量 0
drwxr-xr-x. 2 root root 6 3月 20 14:59 mydockerfile
# 创建dockerfile文件:Dockerfile_centos
# 作为wokong_centos镜像的dockerfile文件。
[root@192 mydockerfile]# vim Dockerfile_centos
提示:Dockerfile文件可以自定义。
Dockerfile文件内容如下:
FROM centos # 继承了本地的centos镜像
MAINTAINER wukong<wukong6688@126.com> # 镜像作者的名字和邮箱信息
ENV MYPATH /tmp # 配置环境变量,是一个key:value模式的键值对存储。也可以用=号。
WORKDIR $MYPATH # 定义容器初始工作目录,并引用了上边的ENV变量。
RUN yum -y install vim # 容器构建时需要运行的命令。
RUN yum -y install net-tools
EXPOSE 80 # 暴露80端口到服务器
CMD echo "success---------ok"
CMD /bin/bash
说明:
我自定义的
wokong_centos
镜像是以CentOS官方镜像为基础进行修改的。而CentOS官方镜像的Base镜像是
FROM scratch
。Docker Hub中99%的镜像都是通过
scratch
镜像,作为最底层的镜像,然后安装和配置出所需要的软件,最后得到我们需要的镜像。(
scratch
镜像是一个空的镜像,可以真正的从零开始构建属于自己的镜像。)
3、构建镜像
执行命令:docker build -f Dockerfile文件路径 -t 新镜像名字:TAG .
安装过程如下:
# 完整安装过程
[root@192 mydockerfile]# docker build -f /home/mydockerfile/Dockerfile_centos \
> -t wokong_centos:1.6 .
Sending build context to Docker daemon 2.048kB
Step 1/9 : FROM centos
---> 300e315adb2f
Step 2/9 : MAINTAINER wukong<wukong6688@126.com>
---> Running in 38481c4819a3
Removing intermediate container 38481c4819a3
---> 96a9036d8cad
Step 3/9 : ENV MYPATH /tmp
---> Running in bb236291ff01
Removing intermediate container bb236291ff01
---> 6cf48c1a740a
Step 4/9 : WORKDIR $MYPATH
---> Running in 15ce861a59c1
Removing intermediate container 15ce861a59c1
---> 537898233556
Step 5/9 : RUN yum -y install vim
---> Running in 99f06b342bf8
CentOS Linux 8 - AppStream 1.8 MB/s | 6.3 MB 00:03
CentOS Linux 8 - BaseOS 1.7 MB/s | 2.3 MB 00:01
CentOS Linux 8 - Extras 13 kB/s | 9.2 kB 00:00
Last metadata expiration check: 0:00:01 ago on Sat Mar 20 08:10:25 2021.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-15.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-15.el8 appstream 6.3 M
vim-filesystem noarch 2:8.0.1763-15.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: 30 M
Downloading Packages:
(1/5): gpm-libs-1.20.7-15.el8.x86_64.rpm 157 kB/s | 39 kB 00:00
(2/5): vim-enhanced-8.0.1763-15.el8.x86_64.rpm 3.0 MB/s | 1.4 MB 00:00
(3/5): vim-filesystem-8.0.1763-15.el8.noarch.rp 181 kB/s | 48 kB 00:00
(4/5): which-2.21-12.el8.x86_64.rpm 122 kB/s | 49 kB 00:00
(5/5): vim-common-8.0.1763-15.el8.x86_64.rpm 5.7 MB/s | 6.3 MB 00:01
--------------------------------------------------------------------------------
Total 1.5 MB/s | 7.8 MB 00:05
CentOS Linux 8 - AppStream 1.6 MB/s | 1.6 kB 00:00
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
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-15.el8.noarch 2/5
Installing : vim-common-2:8.0.1763-15.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-15.el8.x86_64 5/5
Running scriptlet: vim-enhanced-2:8.0.1763-15.el8.x86_64 5/5
Running scriptlet: vim-common-2:8.0.1763-15.el8.x86_64 5/5
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/5
Verifying : vim-common-2:8.0.1763-15.el8.x86_64 2/5
Verifying : vim-enhanced-2:8.0.1763-15.el8.x86_64 3/5
Verifying : vim-filesystem-2:8.0.1763-15.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-15.el8.x86_64
vim-enhanced-2:8.0.1763-15.el8.x86_64 vim-filesystem-2:8.0.1763-15.el8.noarch
which-2.21-12.el8.x86_64
Complete!
Removing intermediate container 99f06b342bf8
---> c58485da8447
Step 6/9 : RUN yum -y install net-tools
---> Running in a5e4191e9fb2
Last metadata expiration check: 0:00:19 ago on Sat Mar 20 08:10:25 2021.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
net-tools x86_64 2.0-0.52.20160912git.el8 baseos 322 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 322 k
Installed size: 942 k
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm 1.7 MB/s | 322 kB 00:00
--------------------------------------------------------------------------------
Total 195 kB/s | 322 kB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Verifying : net-tools-2.0-0.52.20160912git.el8.x86_64 1/1
Installed:
net-tools-2.0-0.52.20160912git.el8.x86_64
Complete!
Removing intermediate container a5e4191e9fb2
---> 5e7dba859606
Step 7/9 : EXPOSE 80
---> Running in e5b3c11088d3
Removing intermediate container e5b3c11088d3
---> 5d8199d84409
Step 8/9 : CMD echo "success---------ok"
---> Running in 0d8b83e6008b
Removing intermediate container 0d8b83e6008b
---> 94ff44f5e94c
Step 9/9 : CMD /bin/bash
---> Running in 31a13253e359
Removing intermediate container 31a13253e359
---> 6441f63090d4
Successfully built 6441f63090d4
Successfully tagged wokong_centos:1.6
# 查看本地Docker镜像。
[root@192 mydockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wokong_centos 1.6 6441f63090d4 10 seconds ago 291MB
centos latest 300e315adb2f 3 months ago 209MB
我们可以看到新构建的wokong_centos
镜像,比官方CentOS镜像多了80多M。
说明:
docker build
命令最后有一个.
,这个.
有的说表示当前目录,有的说表示将当前目录的上下文路径中使用Dockerfile构建。安装后一种说法,上面的构建命令中,
.
也可以替换成/home/mydockerfile/
。(我尝试了,可行)但你就记住是固定写法就好了。
4、运行镜像
我们把刚刚构建的wokong_centos
镜像启动起来。
# 启动进入wokong_centos容器
[root@192 mydockerfile]# docker run -it wokong_centos:1.6
[root@55e0b688c9fb tmp]#
# 1.查看默认工作路径
[root@55e0b688c9fb tmp]# pwd
/tmp
# 2.查看当前容器IP
[root@55e0b688c9fb tmp]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 8 bytes 656 (656.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
# 3.查看是否可以使用vim编辑器
[root@55e0b688c9fb tmp]# vim --help
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jun 18 2020 15:49:08)
usage: vim [arguments] [file ..] edit specified file(s)
or: vim [arguments] - read text from stdin
or: vim [arguments] -t tag edit file where tag is defined
or: vim [arguments] -q [errorfile] edit file with first error
我们可以看到,进入容器的默认工作目录变成了/tmp
目录了,而不是之前根目录了。
同时ifconfig
命令,和vim编辑器都可以使用了。
5、列出镜像的变更历史
命令:docker history 镜像名/ID
。
作用:可以列出本地镜像的变更历史。
示例:
# 查看本地Docker镜像
[root@192 mydockerfile]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
wokong_centos 1.6 6441f63090d4 17 minutes ago 291MB
centos latest 300e315adb2f 3 months ago 209MB
# 查看wokong_centos镜像的变更历史
[root@192 mydockerfile]# docker history wokong_centos:1.6
IMAGE CREATED CREATED BY SIZE COMMENT
6441f63090d4 18 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
94ff44f5e94c 18 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo… 0B
5d8199d84409 18 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B
5e7dba859606 18 minutes ago /bin/sh -c yum -y install net-tools 23.3MB
c58485da8447 18 minutes ago /bin/sh -c yum -y install vim 58MB
537898233556 18 minutes ago /bin/sh -c #(nop) WORKDIR /tmp 0B
6cf48c1a740a 18 minutes ago /bin/sh -c #(nop) ENV MYPATH=/tmp 0B
96a9036d8cad 18 minutes ago /bin/sh -c #(nop) MAINTAINER wukong<wukong6… 0B
300e315adb2f 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:bd7a2aed6ede423b7… 209MB