0%

Docker|4.Dockerfile编写

Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本。

Dockerfile构建过程解析

Dockerfile基础知识

  • 每条保留指令都必须为大写字母且后面要跟随至少一个参数
  • 指令按照从上到下,顺序执行
  • #表示注释
  • 每条指令都会创建一个新的镜像层,并对镜像进行提交

Docker执行Dockerfile的大概流程

  • docker从基础镜像运行一个容器
  • 执行一条指令并对容器做出修改
  • 执行类似docker commit的操作提交一个新的镜像层
  • docker再基于刚提交的镜像运行一个新容器
  • 执行dockerfile中的下一条指令直到所有指令都执行完成

Dockerfile,需要定义一个Dockerfile,Dockerfile定义了进程需要的一切东西。Dockerfile涉及的内容包括执行代码或者是文件、环境变量、依赖包、运行时环境、动态链接库、操作系统的发行版、服务进程和内核进程(当应用进程需要和系统服务和内核进程打交道,这时需要考虑如何设计namespace的权限控制)等等;

Docker镜像,在用Dockerfile定义一个文件之后,docker build时会产生一个Docker镜像,当运行Docker镜像时,会真正开始提供服务;

Docker容器,容器是直接提供服务的。

Dockerfile体系结构

保留字指令含义
FROM基础镜像,当前新镜像是基于哪个镜像的
MAINTAINER镜像维护者的姓名和邮箱地址
RUN容器构建时需要运行的指令
EXPOSE容器对外暴漏的端口
WORKDIR指定在创建容器后,终端默认登录进来的工作目录,一个落脚点
ENV在构建镜像过程中设置环境变量
ADD将宿主机目录下的文件拷贝进镜像且ADD命令会自动处理URL和解压tar压缩包
COPY类似ADD,拷贝文件和目录到镜像中。将从构建上下文目录中的<原路径>的文件/目标复制到新的一层的镜像内的<目标路径>位置
VOLUME容器数据卷,用于数据保存和持久化工作
CMD指定一个容器启动时要运行的命令。Dockfile中可以有多个CMD指令,但只有最后一个生效,CMD会被docker run之后的参数替换(替换)
ENTRYPOINT指定一个容器启动时要运行的命令。ENTRYPOINT的目标和CMD一样,都是在指定容器启动程序及参数(追加)
ONBUILD当构建一个被继承的Dockerfile时运行命令,父镜像在被子镜像继承后父镜像的onbuild被触发。

编写tomcat dockerfile

1
2
3
4
5
6
7
8
[root@localhost tomcat]# pwd
/root/mydocker/tomcat
[root@localhost tomcat]# ls -l
total 199896
-rw-r--r-- 1 root root 9584807 Jul 6 2018 apache-tomcat-8.5.32.tar.gz
-rw-r--r-- 1 root root 5 Feb 13 00:15 c.txt
-rw-r--r-- 1 root root 614 Feb 13 00:21 dockerfile
-rw-r--r-- 1 root root 195094741 Oct 31 15:00 jdk-8u221-linux-x64.tar.gz
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost tomcat]# cat /root/mydocker/tomcat/dockerfile 
FROM centos
MAINTAINER arlo<10887272@qq.com>
WORKDIR /usr/local
#安装vim,lsof软件
RUN yum -y install vim lsof
#拷贝并解压jdk,tomcat
COPY c.txt /usr/local/ctext.txt
ADD jdk-8u221-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-8.5.32.tar.gz /usr/local/
#配置环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_221/
ENV CLASSPATH .:$JAVA_HOME/lib/:$JAVA_HOME/jre/lib/
ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.32
ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.32
ENV PATH $JAVA_HOME/bin:$PATH
#暴漏容器服务端口
EXPOSE 8080
#启动服务
CMD ["/usr/local/apache-tomcat-8.5.32/bin/catalina.sh","run"]
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
[root@localhost tomcat]# docker build -f /root/mydocker/tomcat/dockerfile -t test/tomcat .
Sending build context to Docker daemon 204.7MB
Step 1/14 : FROM centos
---> 470671670cac
Step 2/14 : MAINTAINER arlo<10887272@qq.com>
---> Using cache
---> 9fe35c7393cf
Step 3/14 : WORKDIR /usr/local
---> Running in 32918030b909
Removing intermediate container 32918030b909
---> d3d46c9c0512
Step 4/14 : RUN yum -y install vim lsof
---> Running in 897a79906fc7
CentOS-8 - AppStream 251 kB/s | 6.4 MB 00:26
CentOS-8 - Base 1.3 MB/s | 5.0 MB 00:03
CentOS-8 - Extras 907 B/s | 2.1 kB 00:02
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim-enhanced x86_64 2:8.0.1763-13.el8 AppStream 1.4 M
lsof x86_64 4.91-2.el8 BaseOS 253 k
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-10.el8 BaseOS 49 k

Transaction Summary
================================================================================
Install 6 Packages

Total download size: 8.1 M
Installed size: 31 M
Downloading Packages:
(1/6): gpm-libs-1.20.7-15.el8.x86_64.rpm 212 kB/s | 39 kB 00:00
(2/6): vim-filesystem-8.0.1763-13.el8.noarch.rp 961 kB/s | 48 kB 00:00
(3/6): lsof-4.91-2.el8.x86_64.rpm 530 kB/s | 253 kB 00:00
(4/6): which-2.21-10.el8.x86_64.rpm 918 kB/s | 49 kB 00:00
(5/6): vim-common-8.0.1763-13.el8.x86_64.rpm 2.5 MB/s | 6.3 MB 00:02
(6/6): vim-enhanced-8.0.1763-13.el8.x86_64.rpm 538 kB/s | 1.4 MB 00:02
--------------------------------------------------------------------------------
Total 2.0 MB/s | 8.1 MB 00:03
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
CentOS-8 - AppStream 1.6 MB/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-10.el8.x86_64 1/6
Installing : vim-filesystem-2:8.0.1763-13.el8.noarch 2/6
Installing : vim-common-2:8.0.1763-13.el8.x86_64 3/6
Installing : gpm-libs-1.20.7-15.el8.x86_64 4/6
Running scriptlet: gpm-libs-1.20.7-15.el8.x86_64 4/6
Installing : vim-enhanced-2:8.0.1763-13.el8.x86_64 5/6
Installing : lsof-4.91-2.el8.x86_64 6/6
Running scriptlet: lsof-4.91-2.el8.x86_64 6/6
Running scriptlet: vim-common-2:8.0.1763-13.el8.x86_64 6/6
Verifying : gpm-libs-1.20.7-15.el8.x86_64 1/6
Verifying : vim-common-2:8.0.1763-13.el8.x86_64 2/6
Verifying : vim-enhanced-2:8.0.1763-13.el8.x86_64 3/6
Verifying : vim-filesystem-2:8.0.1763-13.el8.noarch 4/6
Verifying : lsof-4.91-2.el8.x86_64 5/6
Verifying : which-2.21-10.el8.x86_64 6/6

Installed:
vim-enhanced-2:8.0.1763-13.el8.x86_64 lsof-4.91-2.el8.x86_64
gpm-libs-1.20.7-15.el8.x86_64 vim-common-2:8.0.1763-13.el8.x86_64
vim-filesystem-2:8.0.1763-13.el8.noarch which-2.21-10.el8.x86_64

Complete!
Removing intermediate container 897a79906fc7
---> 13121fca3153
Step 5/14 : COPY c.txt /usr/local/ctext.txt
---> 295b1db8995d
Step 6/14 : ADD jdk-8u221-linux-x64.tar.gz /usr/local/
---> 8f55fab3cee8
Step 7/14 : ADD apache-tomcat-8.5.32.tar.gz /usr/local/
---> 19390cab29cb
Step 8/14 : ENV JAVA_HOME /usr/local/jdk1.8.0_221/
---> Running in d7db7c594c17
Removing intermediate container d7db7c594c17
---> 0e316b643b87
Step 9/14 : ENV CLASSPATH .:$JAVA_HOME/lib/:$JAVA_HOME/jre/lib/
---> Running in fb8be4c72efe
Removing intermediate container fb8be4c72efe
---> 9b2d22114da9
Step 10/14 : ENV CATALINA_HOME /usr/local/apache-tomcat-8.5.32
---> Running in 0026d43d76ef
Removing intermediate container 0026d43d76ef
---> 1780fa4f640e
Step 11/14 : ENV CATALINA_BASE /usr/local/apache-tomcat-8.5.32
---> Running in 2c843fa05104
Removing intermediate container 2c843fa05104
---> b3e0da022f22
Step 12/14 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in 7366d7cb77a1
Removing intermediate container 7366d7cb77a1
---> afefbc99ba9c
Step 13/14 : EXPOSE 8080
---> Running in a65838b19573
Removing intermediate container a65838b19573
---> cf9fbf0a77e9
Step 14/14 : CMD ["/usr/local/apache-tomcat-8.5.32/bin/startup.sh","run"]
---> Running in 51d8114e1305
Removing intermediate container 51d8114e1305
---> fd478a528ca4
Successfully built fd478a528ca4
Successfully tagged test/tomcat:latest
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
[root@localhost tomcat]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/tomcat latest fd478a528ca4 32 seconds ago 721MB
test/centos2 latest 9c47c178eb61 3 hours ago 314MB
test/centos latest c8ed8caf62e2 17 hours ago 237MB
tomcat latest b56d8850aed5 5 days ago 529MB
centos latest 470671670cac 3 weeks ago 237MB
[root@localhost tomcat]# docker history test/tomcat
IMAGE CREATED CREATED BY SIZE COMMENT
fd478a528ca4 42 seconds ago /bin/sh -c #(nop) CMD ["/usr/local/apache-t… 0B
cf9fbf0a77e9 46 seconds ago /bin/sh -c #(nop) EXPOSE 8080 0B
afefbc99ba9c 49 seconds ago /bin/sh -c #(nop) ENV PATH=/usr/local/jdk1.… 0B
b3e0da022f22 53 seconds ago /bin/sh -c #(nop) ENV CATALINA_BASE=/usr/lo… 0B
1780fa4f640e 57 seconds ago /bin/sh -c #(nop) ENV CATALINA_HOME=/usr/lo… 0B
9b2d22114da9 About a minute ago /bin/sh -c #(nop) ENV CLASSPATH=.:/usr/loca… 0B
0e316b643b87 About a minute ago /bin/sh -c #(nop) ENV JAVA_HOME=/usr/local/… 0B
19390cab29cb About a minute ago /bin/sh -c #(nop) ADD file:047cf33d1eac02dd1… 13.6MB
8f55fab3cee8 About a minute ago /bin/sh -c #(nop) ADD file:cd29f38ce9a4a50a3… 407MB
295b1db8995d About a minute ago /bin/sh -c #(nop) COPY file:ba9516c02edf6166… 5B
13121fca3153 About a minute ago /bin/sh -c yum -y install vim lsof 63.6MB
d3d46c9c0512 2 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B
9fe35c7393cf 3 hours ago /bin/sh -c #(nop) MAINTAINER arlo<10887272@… 0B
470671670cac 3 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B
<missing> 3 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc… 0B
<missing> 4 weeks ago /bin/sh -c #(nop) ADD file:aa54047c80ba30064… 237MB
1
[root@localhost tomcat]# docker run -d -p 8888:8080 --name tomcat0212 -v /data/tomcatwar:/usr/local/apache-tomcat-8.5.32/webapps -v /data/tomcatlogs:/usr/local/apache-tomcat-8.5.32/logs --privileged=true test/tomcat