Docker 之七实战 Dockerfile 的简单编写

实战内容介绍

实战编写 Dockerfile,指定终端登录后的默认路径,同时预安装 vim、ifconfig 工具,最后通过 Dockerfile 构建新的 Centos 镜像并运行起来。

编写 Dockerfile 文件

1
2
3
4
5
6
7
8
9
10
FROM centos

MAINTAINER peter<peter@gmail.com>

ENV work_path /usr/local
WORKDIR $work_path

RUN yum -y update && yum -y install vim net-tools

CMD /bin/bash

构建新的 Docker 镜像

1
2
3
4
5
6
7
8
9
10
11
# 拉取最新的Centos镜像
# docker pull centos

# 创建Dockerfile文件,并写入上述的文件内容
# vi ~/dockerfile-centos

# 通过Dockerfile文件构建新的Docker镜像
# docker build -f ~/dockerfile-centos -t peter/centos:1.1 .

# 可以使用--no-cache参数让Docker构建镜像时不使用缓存(中间镜像)
# docker build --no-cache=True -f ~/dockerfile-centos -t peter/centos:1.1 .

构建新 Docker 镜像时输出的日志信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Sending build context to Docker daemon  93.82MB
Step 1/8 : FROM centos
---> 1e1148e4cc2c
Step 2/8 : MAINTAINER peter<peter@gmail.com>
---> Running in bff9f4e99fc1
Removing intermediate container bff9f4e99fc1
---> 058e53021a24
Step 3/8 : ENV work_path /usr/local
---> Running in 33e3edef7380
Removing intermediate container 33e3edef7380
---> 52f1709ed735
Step 4/8 : WORKDIR $work_path
---> Running in 1304b1642e5a
Removing intermediate container 1304b1642e5a
---> a158527938a7
Step 5/8 : RUN yum -y update
---> Running in 654318bc1889
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
* base: mirror.jdcloud.com
* extras: mirror.jdcloud.com
* updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
---> Package systemd.x86_64 0:219-62.el7 will be updated
---> Package systemd.x86_64 0:219-62.el7_6.2 will be an update
---> Package systemd-libs.x86_64 0:219-62.el7 will be updated
---> Package systemd-libs.x86_64 0:219-62.el7_6.2 will be an update
---> Package tzdata.noarch 0:2018g-1.el7 will be updated
---> Package tzdata.noarch 0:2018i-1.el7 will be an update
--> Finished Dependency Resolution

...(省略)

Step 7/8 : EXPOSE 80
---> Running in 4aae7e66419c
Removing intermediate container 4aae7e66419c
---> b5647b33ffb8
Step 8/8 : CMD /bin/bash
---> Running in 877a21e5c705
Removing intermediate container 877a21e5c705
---> 90fed978ec5c
Successfully built 90fed978ec5c
Successfully tagged peter/centos:1.1

查看新构建的 Docker 镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 查看新的Docker镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
peter/centos 1.1 8544847124e9 3 minutes ago 385MB
centos latest 1e1148e4cc2c 6 weeks ago 202MB

# 查看新Docker镜像的构建历史
# docker history peter/centos:1.1
IMAGE CREATED CREATED BY SIZE COMMENT
90fed978ec5c 2 hours ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin… 0B
b5647b33ffb8 2 hours ago /bin/sh -c #(nop) EXPOSE 80 0B
3c621d86e6de 2 hours ago /bin/sh -c yum -y install vim net-tools 79.5MB
a83741b44f7b 2 hours ago /bin/sh -c yum -y update 104MB
a158527938a7 2 hours ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
52f1709ed735 2 hours ago /bin/sh -c #(nop) ENV work_path=/usr/local 0B
058e53021a24 2 hours ago /bin/sh -c #(nop) MAINTAINER peter<peter@gm… 0B
1e1148e4cc2c 6 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 6 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 6 weeks ago /bin/sh -c #(nop) ADD file:6f877549795f4798a… 202MB

以交互方式运行新构建的 Docker 镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 以交互式运行新的Docker镜像
# docker run -it --name="peter-centos" peter/centos:1.1

# 退出终端(不停止容器)
# ctrl + p + q

# 查看当前所有正在运行的Docker容器
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1a8c8ac1efc peter/centos:1.1 "/bin/sh -c /bin/bash" 2 minutes ago Up 2 minutes 80/tcp peter-centos

# 重新连接到Docker容器,连接之后如果想断开连接,在容器内的终端直接执行"exit"命令即可,连接断开后容器不会停止运行
# docker exec -it peter-centos /bin/bash

# 注意:如果以后台方式运行上面新构建的Docker镜像,当容器启动完成后会马上关闭,因为Docker会认为容器内没有需要运行的应用(指当前容器内没有前台运行的进程)。