8.Dockerfile详解【2】

 

Dockerfile详解【2】

1. Base镜像的说明

docker Hub上99%的镜像都是通过base镜像中安装和配置需要的软件构建出来的。

2. 自定义mycentos镜像

2.1 目标:

  1. 登陆后的默认路径
  2. vim编辑器

2.2 进入mydocker目录创建DockerFile,文件编写

1
2
3
4
[root@localhost centos]# pwd
/usr/local/docker/docker_file/centos
[root@localhost centos]# touch Dockerfile
[root@localhost centos]# vim Dockerfile

Dockerfile里面写入内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# base image
FROM centos
# author and email
MAINTAINER Tom<Tom@qq.com>
# setup environment variable
ENV MYPATH /usr/local
# work directory
WORKDIR ${MYPATH}
# install vim
RUN yum -y install vim
# port
EXPOSE 80
# output
CMD echo ${MYPATH}
CMD echo "success, finished!"
CMD /bin/bash

2.3 使用docker build命令构建镜像

1
[root@localhost centos]# docker build -t mycentos01:2.0 .

container id: 814f0d94c86b, image id: e91b1474c962

output:

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
Sending build context to Docker daemon 2.048 kB
Step 1/9 : FROM centos
---> 0d120b6ccaa8
Step 2/9 : MAINTAINER Tom<Tom@qq.com>
---> Running in 814f0d94c86b
---> e91b1474c962
Removing intermediate container 814f0d94c86b
Step 3/9 : ENV MYPATH /usr/local
---> Running in 96525b6c63e9
---> bacb4c16c1f0
Removing intermediate container 96525b6c63e9
Step 4/9 : WORKDIR ${MYPATH}
---> d622aa686883
Removing intermediate container a9cf9ea40f1f
Step 5/9 : RUN yum -y install vim
---> Running in bd65d6cdd4e1

CentOS-8 - AppStream 3.4 MB/s | 5.8 MB 00:01
CentOS-8 - Base 1.1 MB/s | 2.2 MB 00:02
CentOS-8 - Extras 10 kB/s | 8.6 kB 00:00
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 623 kB/s | 39 kB 00:00
(2/5): vim-filesystem-8.0.1763-13.el8.noarch.rp 848 kB/s | 48 kB 00:00
(3/5): which-2.21-12.el8.x86_64.rpm 332 kB/s | 49 kB 00:00
(4/5): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 411 kB/s | 1.4 MB 00:03
(5/5): vim-common-8.0.1763-13.el8.x86_64.rpm 398 kB/s | 6.3 MB 00:16
--------------------------------------------------------------------------------
Total 430 kB/s | 7.8 MB 00:18
warning: /var/cache/dnf/AppStream-02e86d1c976ab532/packages/gpm-libs-1.20.7-15.e l8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS-8 - AppStream 17 kB/s | 1.6 kB 00:00
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-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!
---> 160bf8d94eca
Removing intermediate container bd65d6cdd4e1
Step 6/9 : EXPOSE 80
---> Running in 5c4ba5591c52
---> c12aa2483683
Removing intermediate container 5c4ba5591c52
Step 7/9 : CMD echo ${MYPATH}
---> Running in 0b9240516a40
---> b91d27669bd4
Removing intermediate container 0b9240516a40
Step 8/9 : CMD echo "success, finished!"
---> Running in 99a9807164f3
---> 384b4018892b
Removing intermediate container 99a9807164f3
Step 9/9 : CMD /bin/bash
---> Running in 89d99aa2efd7
---> f503701c7737
Removing intermediate container 89d99aa2efd7
Successfully built f503701c7737
1
2
3
[root@localhost centos]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mycentos01 2.0 f503701c7737 22 minutes ago 272 MB

2.4 使用docker run命令运行

1
[root@localhost centos]# docker run -it mycentos01:2.0

2.5 列出镜像的变更历史

 
docker history 镜像名:TAG

1
[root@localhost centos]# docker history mycentos01:2.0
1
2
3
4
5
6
7
8
9
10
11
12
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
f503701c7737 23 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/b... 0 B
384b4018892b 23 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "ec... 0 B
b91d27669bd4 23 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "ec... 0 B
c12aa2483683 23 minutes ago /bin/sh -c #(nop) EXPOSE 80/tcp 0 B
160bf8d94eca 23 minutes ago /bin/sh -c yum -y install vim 57.3 MB
d622aa686883 24 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0 B
bacb4c16c1f0 24 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0 B
e91b1474c962 24 minutes ago /bin/sh -c #(nop) MAINTAINER Tom<Tom@qq.com> 0 B
0d120b6ccaa8 3 months ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
<missing> 3 months ago /bin/sh -c #(nop) LABEL org.label-schema.... 0 B
<missing> 3 months ago /bin/sh -c #(nop) ADD file:538afc0c5c964ce... 215 MB