九Dockerfile

Posted zhuzhaoli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了九Dockerfile相关的知识,希望对你有一定的参考价值。

Docker 可以通过 Dockerfile 的内容来自动构建镜像。Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本,通过docker build命令可以根据 Dockerfile 的内容构建镜像

9.1 Dockerfile构建过程解析

9.1.1 Dockerfile内容基础知识

(1)每条保留字指令都必须为大写字母且后面跟至少一个参数

(2)指令按照从上到下,顺序执行

(3)#表示注释

(4)每条指令都会创建一个新的镜像层,并对镜像层进行提交

9.1.2 Docker执行Dockerfile的大致流程

(1)docker从基础镜像运行一个容器

(2)执行一条指令并对容器作出修改

(3)执行类似docker commit的操作提交一个新的镜像层

(4)docker再基于刚提交的镜像运行一个新容器

(5)执行dockerfile中的下一条指令直到所有指令都执行完成

9.1.3 Dockerfile、Docker镜像和Docker容器关系

从应用软件的角度来看,Dockerfile、Docker和Docker容器分别代表软件的三个不同阶段:

Dockerfile是软件的原材料

Docker镜像是软件的交付品

Docker容器是软件的运行态

Dockerfile面向开发,Docker镜像成为交付标准,Docker容器则涉及部署与运维,三者缺一不可,合力充当Docker体系的基础。

9.2 Dockerfile体系结构(保留关键字)

9.2.1 FROM

FROM <image>

FROM指定构建镜像的基础源镜像,如果本地没有指定的镜像,则会自动从 Docker 的公共库 pull 镜像下来。

FROM必须是 Dockerfile 中非注释行的第一个指令,即一个 Dockerfile 从FROM语句开始。

FROM可以在一个 Dockerfile 中出现多次,如果有需求在一个 Dockerfile 中创建多个镜像。

如果FROM语句没有指定镜像标签,则默认使用latest标签。

9.2.2 MAINTAINER

MAINTAINER <name>

镜像维护者的姓名和邮箱地址

9.2.3 RUN

容器构建时需要运行的命令

RUN "executable", "param1", "param2"

每条RUN指令将在当前镜像基础上执行指定命令,并提交为新的镜像,后续的RUN都在之前RUN提交后的镜像为基础,镜像是分层的,可以通过一个镜像的任何一个历史提交点来创建,类似源码的版本控制。

exec 方式会被解析为一个 JSON 数组,所以必须使用双引号而不是单引号。exec 方式不会调用一个命令 shell,所以也就不会继承相应的变量,如:

RUN [ "echo", "$HOME" ]

这种方式是不会达到输出 HOME 变量的,正确的方式应该是这样的

RUN [ "sh", "-c", "echo", "$HOME" ]

RUN产生的缓存在下一次构建的时候是不会失效的,会被重用,可以使用--no-cache选项,即docker build --no-cache,如此便不会缓存。

9.2.4 CMD

CMD有三种使用方式:

CMD "executable","param1","param2"

CMD "param1","param2"

CMD command param1 param2 (shell form)

指定一个容器启动时要运行的命令,Dockerfile中可以有多个CMD指令,但只有最后一个生效,CMD会被docker run之后的参数替换。

CMD的目的是为了在启动容器时提供一个默认的命令执行选项。如果用户启动容器时指定了运行的命令,则会覆盖掉CMD指定的命令。

CMD会在启动容器的时候执行,build 时不执行,而RUN只是在构建镜像的时候执行,后续镜像构建完成之后,启动容器就与RUN无关了

9.2.5 EXPOSE

EXPOSE <port> [<port>...]

告诉 Docker 服务端容器对外映射的本地端口,需要在 docker run 的时候使用-p或者-P选项生效。

9.2.6 ENV

ENV <key> <value>       # 只能设置一个变量

ENV <key>=<value> ...   # 允许一次设置多个变量

指定一个环境变量,会被后续RUN指令使用,并在容器运行时保留。

9.2.7 ADD

ADD <src>... <dest>

将宿主机目录下的文件拷贝进镜像且ADD命令会自动处理URL和解压tar压缩包

路径必须是绝对路径,如果不存在,会自动创建对应目录

路径必须是 Dockerfile 所在路径的相对路径

如果是一个目录,只会复制目录下的内容,而目录本身则不会被复制

9.2.8 COPY

COPY <src>... <dest>

类似ADD,拷贝文件和目录到镜像中,将从构建上下文目录中<源路径>的文件/目录复制到新的一层的镜像内的<目标路径>位置

9.2.9 ENTRYPOINT

ENTRYPOINT "executable", "param1", "param2"

ENTRYPOINT command param1 param2 (shell form)

配置容器启动后执行的命令,并且不可被 docker run 提供的参数覆盖,而CMD是可以被覆盖的。如果需要覆盖,则可以使用docker run --entrypoint选项。

每个 Dockerfile 中只能有一个ENTRYPOINT,当指定多个时,只有最后一个生效。

9.2.10 VOLUME

VOLUME ["/data"]

容器数据卷,用于数据保存和持久化工作

9.2.11 WORKDIR

WORKDIR /path/to/workdir

指定在创建容器后,终端默认登录进来的工作目录,一个落脚点。

9.2.12 ONBUILD

ONBUILD [INSTRUCTION]

当构建一个被继承的Dockerfile时运行命令,父镜像在被子继承后父镜像的onbuild被触发。

9.3 dockerfile 最佳实践

9.3.1 DockerFile案例1

Base镜像(scratch)

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

自定义镜像myCentOS

1、编写

Docker Hub默认CentOS镜像什么情况

[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS               NAMES
25501c70bdf8        boxiaoyuan/centos   "/bin/sh -c /bin/bas   47 minutes ago      Up 47 minutes                           boxiaoyuan_sub      
[[email protected] ~]# docker rm -f $(docker ps -q) 
25501c70bdf8
[[email protected] ~]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[[email protected] ~]# docker run -it centos /bin/bash
[[email protected] /]# pwd
/
[[email protected] /]# vim a.txt
bash: vim: command not found
[[email protected] /]# ifconfig
bash: ifconfig: command not found
[[email protected] /]# 

可以发现,Docker Hub的CentOS为何只有200MB,而宿主机的CentOS有4个G,Docker Hub上的是去除了很多不需要的东西,只保留了重要的内核文件的一个精简版的CentOS。

因此,我们来写个自定义的CentOS镜像,具备如下:

登陆后的默认路径

vim编辑器

查看网络配置ifconfig支持

2、准备编写DockerFile文件

创建Dockerfile2

[[email protected] mydocker]# vim Dockerfile2
[[email protected] mydocker]# cat Dockerfile2 
FROM centos
MAINTAINER boxiaoyuan<[email protected]>

ENV MYPATH /usr/local
WORKDIR $MYPATH

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

EXPOSE 80

CMD echo $MYPATH
CMD echo "success================ok"
CMD /bin/bash

[[email protected] mydocker]# 

创建镜像

不要忘记最后的 .

[[email protected] mydocker]# docker build -f /mydocker/Dockerfile2 -t mycentos:1.0 .
Sending build context to Docker daemon 3.072 kB
Sending build context to Docker daemon 
Step 0 : FROM centos
 ---> ee2526f4865b
Step 1 : MAINTAINER boxiaoyuan<[email protected]>
 ---> Running in 9ce8bfee5913
 ---> b3b3b019684f
Removing intermediate container 9ce8bfee5913
Step 2 : ENV MYPATH /usr/local
 ---> Running in 9d218ebcfefc
 ---> d0a105c898d0
Removing intermediate container 9d218ebcfefc
Step 3 : WORKDIR $MYPATH
 ---> Running in 22aaf424324c
 ---> 0ce63b1bf674
Removing intermediate container 22aaf424324c
Step 4 : RUN yum -y install vim
 ---> Running in 57320c2e95d6
Complete!
 ---> a71d6a505ec3
Removing intermediate container 57320c2e95d6
Step 5 : RUN yum -y install net-tools
 ---> Running in b7835e8c125c                                  
Complete!
 ---> fa70420e4838
Removing intermediate container b7835e8c125c
Step 6 : EXPOSE 80
 ---> Running in f7c84d5c7733
 ---> 0b3fb395212c
Removing intermediate container f7c84d5c7733
Step 7 : CMD echo $MYPATH
 ---> Running in dabdc663af3b
 ---> 21a03c348b69
Removing intermediate container dabdc663af3b
Step 8 : CMD echo "success================ok"
 ---> Running in 7fee6ee7dca3
 ---> 45a88a1495ef
Removing intermediate container 7fee6ee7dca3
Step 9 : CMD /bin/bash
 ---> Running in 9baf10923912
 ---> d200a3402f0f
Removing intermediate container 9baf10923912
Successfully built d200a3402f0f

生成镜像完毕

[[email protected] mydocker]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mycentos                  1.0                 d200a3402f0f        2 minutes ago       385.7 MB
boxiaoyuan/centos         latest              9f3d5ff4c0b1        About an hour ago   201.8 MB
boxiaoyuan/nodocstomcat   1.0                 002a5c87a638        2 hours ago         510.4 MB
tomcat                    latest              d06128b13608        2 days ago          510.3 MB
ubuntu                    latest              d351d647d655        2 weeks ago         64.18 MB
ubuntu                    14.04               fe62d34455b7        7 weeks ago         188.2 MB
centos                    6                   4f2ed42dccff        3 months ago        193.9 MB
centos                    latest              ee2526f4865b        3 months ago        201.8 MB
hello-world               latest              9f5834b25059        6 months ago        1.84 kB

运行镜像

[[email protected] mydocker]# docker run -it mycentos:1.0
[[email protected] local]# pwd
/usr/local
[[email protected] local]# ls -al
total 48
drwxr-xr-x. 12 root root 4096 Mar  5 17:34 .
drwxr-xr-x. 13 root root 4096 Mar  5 17:34 ..
drwxr-xr-x.  2 root root 4096 Apr 11  2018 bin
drwxr-xr-x.  2 root root 4096 Apr 11  2018 etc
drwxr-xr-x.  2 root root 4096 Apr 11  2018 games
drwxr-xr-x.  2 root root 4096 Apr 11  2018 include
drwxr-xr-x.  2 root root 4096 Apr 11  2018 lib
drwxr-xr-x.  2 root root 4096 Apr 11  2018 lib64
drwxr-xr-x.  2 root root 4096 Apr 11  2018 libexec
drwxr-xr-x.  2 root root 4096 Apr 11  2018 sbin
drwxr-xr-x.  5 root root 4096 Mar  5 17:34 share
drwxr-xr-x.  2 root root 4096 Apr 11  2018 src
[[email protected] local]# vim test.txt
[[email protected] local]# cat test.txt
test
[[email protected] local]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.32  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:acff:fe11:20  prefixlen 64  scopeid 0x20<link>
        ether 02:42:ac:11:00:20  txqueuelen 0  (Ethernet)
        RX packets 6  bytes 468 (468.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7  bytes 558 (558.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
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 0  (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

条件都完成了

查看镜像历史

docker history 镜像id/镜像名称

[[email protected] mydocker]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
mycentos                  1.0                 d200a3402f0f        6 minutes ago       385.7 MB
boxiaoyuan/centos         latest              9f3d5ff4c0b1        About an hour ago   201.8 MB
boxiaoyuan/nodocstomcat   1.0                 002a5c87a638        2 hours ago         510.4 MB
tomcat                    latest              d06128b13608        2 days ago          510.3 MB
ubuntu                    latest              d351d647d655        2 weeks ago         64.18 MB
ubuntu                    14.04               fe62d34455b7        7 weeks ago         188.2 MB
centos                    6                   4f2ed42dccff        3 months ago        193.9 MB
centos                    latest              ee2526f4865b        3 months ago        201.8 MB
hello-world               latest              9f5834b25059        6 months ago        1.84 kB
[[email protected] mydocker]# docker history d200a3402f0f
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d200a3402f0f        6 minutes ago       /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin/b   0 B                 
45a88a1495ef        7 minutes ago       /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo \   0 B                 
21a03c348b69        7 minutes ago       /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo $   0 B                 
0b3fb395212c        7 minutes ago       /bin/sh -c #(nop) EXPOSE 80/tcp                 0 B                 
fa70420e4838        7 minutes ago       /bin/sh -c yum -y install net-tools             23.58 MB            
a71d6a505ec3        7 minutes ago       /bin/sh -c yum -y install vim                   160.3 MB            
0ce63b1bf674        9 minutes ago       /bin/sh -c #(nop) WORKDIR /usr/local            0 B                 
d0a105c898d0        9 minutes ago       /bin/sh -c #(nop) ENV MYPATH=/usr/local         0 B                 
b3b3b019684f        9 minutes ago       /bin/sh -c #(nop) MAINTAINER boxiaoyuan<boxia   0 B                 
ee2526f4865b        3 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0 B                 
1f2965b8bcc4        3 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sch   0 B                 
28b0a3c5ee4c        3 months ago        /bin/sh -c #(nop) ADD file:074f2c974463ab38cf   201.8 MB            
[[email protected] mydocker]# 

9.3.2 DockerFile案例2

创建查询ip的镜像

[[email protected] mydocker]# ll
总用量 8
-rw-r--r--. 1 root root 139 7月   6 21:07 Dockerfile
-rw-r--r--. 1 root root 226 7月   6 22:16 Dockerfile2
[[email protected] mydocker]# vim Dockerfile3
[[email protected] mydocker]# cat Dockerfile3
FROM centos
RUN yum install -y curl
CMD ["curl","-s","https://ip.cn"]
[[email protected] mydocker]# docker build -f /mydocker/Dockerfile3 -t myip .
Sending build context to Docker daemon 4.096 kB
Sending build context to Docker daemon 
Step 0 : FROM centos
 ---> ee2526f4865b
Step 1 : RUN yum install -y curl
 ---> Using cache
 ---> 051811f81190
Step 2 : CMD curl -s https://ip.cn
 ---> Using cache
 ---> ffb51f2ac6f6
Successfully built ffb51f2ac6f6

运行容器

[[email protected] mydocker]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
myips                     latest              ffb51f2ac6f6        7 minutes ago       285.5 MB
myip                      latest              ffb51f2ac6f6        7 minutes ago       285.5 MB
<none>                    <none>              f04683ff472a        33 minutes ago      285.5 MB
mycentos                  1.0                 d200a3402f0f        45 minutes ago      385.7 MB
boxiaoyuan/centos         latest              9f3d5ff4c0b1        About an hour ago   201.8 MB
boxiaoyuan/nodocstomcat   1.0                 002a5c87a638        3 hours ago         510.4 MB
tomcat                    latest              d06128b13608        2 days ago          510.3 MB
ubuntu                    latest              d351d647d655        2 weeks ago         64.18 MB
ubuntu                    14.04               fe62d34455b7        7 weeks ago         188.2 MB
centos                    6                   4f2ed42dccff        3 months ago        193.9 MB
centos                    latest              ee2526f4865b        3 months ago        201.8 MB
hello-world               latest              9f5834b25059        6 months ago        1.84 kB
[[email protected] mydocker]# docker run -it myip
"ip": "112.223.21.129", "country": "山东省济南市", "city": "联通"

在宿主机终端执行curl -s -i http://ip.cn

[[email protected] mydocker]# curl -s https://ip.cn
"ip": "112.224.22.119", "country": "山东省济南市", "city": "联通"
[[email protected] mydocker]# curl -s -i https://ip.cn
HTTP/1.1 200 OK
Date: Fri, 12 Jul 2019 11:17:17 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d5af486d4f6a4f7a52f51d091408efd7f1562930237; expires=Sat, 11-Jul-20 11:17:17 GMT; path=/; domain=.ip.cn; HttpOnly
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 4f529c207b4bd372-LAX

"ip": "112.224.22.119", "country": "山东省济南市", "city": "联通"
[[email protected] mydocker]# 

在容器终端执行

[[email protected] mydocker]# docker run myip -i
exec: "-i": executable file not found in $PATH
Error response from daemon: Cannot start container a740b5ac784d41a14278bf40e86697e4f9053d3bbf6443282b4f595d358b46f0: [8] System error: exec: "-i": executable file not found in $PATH

也就是说原本DockerFile中的CMD命令被 -i 替换了,如果要加入 -i 这个命令,则 docker run myip curl -s -i http://ip.cn

制作ENTRYPOINT版可以查询IP信息的容器

创建镜像

[[email protected] mydocker]# vim Dockerfile4
[[email protected] mydocker]# cat Dockerfile4
FROM centos 
RUN yum install -y curl
ENTRYPOINT ["curl","-s","https://ip.cn"]
[[email protected] mydocker]# docker build -f /mydocker/Dockerfile4 -t myip2 .
Sending build context to Docker daemon  5.12 kB
Sending build context to Docker daemon 
Step 0 : FROM centos
 ---> ee2526f4865b
Step 1 : RUN yum install -y curl
 ---> Using cache
 ---> 051811f81190
Step 2 : ENTRYPOINT curl -s https://ip.cn
 ---> Running in e83395370f36
 ---> 68239c983875
Removing intermediate container e83395370f36
Successfully built 68239c983875

运行容器

[[email protected] mydocker]# docker run myip
"ip": "112.224.22.119", "country": "山东省济南市", "city": "联通"

对比下CMD 和 ENTRYPOINT,就可以很清楚的区分两个命令的不同点:前者会被新加的命令覆盖,后者则是在原有的命令后加上新的命令

[[email protected] mydocker]# docker run myip2
"ip": "112.224.22.119", "country": "山东省济南市", "city": "联通"
[[email protected] mydocker]# docker run myip2 -i
HTTP/1.1 200 OK
Date: Fri, 12 Jul 2019 11:23:26 GMT
Content-Type: application/json; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=dc4fb867de9c19c9647b1099858fd12881562930606; expires=Sat, 11-Jul-20 11:23:26 GMT; path=/; domain=.ip.cn; HttpOnly
Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
Server: cloudflare
CF-RAY: 4f52a5248e14785a-LAX

"ip": "112.224.22.119", "country": "山东省济南市", "city": "联通"
[[email protected] mydocker]# 

7.3.3 DockerFile案例3ONBUILD镜像案例

类似触发器,当构建一个被继承的DockerFile时运行命令,父镜像在被子镜像继承后父镜像的ONBUILD被触发。

[[email protected] mydocker]# ll
总用量 16
-rw-r--r--. 1 root root 139 7月   6 21:07 Dockerfile
-rw-r--r--. 1 root root 226 7月   6 22:16 Dockerfile2
-rw-r--r--. 1 root root  70 7月   6 23:05 Dockerfile3
-rw-r--r--. 1 root root  78 7月   6 23:16 Dockerfile4
[[email protected] mydocker]# vim Dockerfile4
[[email protected] mydocker]# cat Dockerfile4
FROM centos 
RUN yum install -y curl
ENTRYPOINT ["curl","-s","https://ip.cn"]
ONBUILD RUN echo "father images onbuild=================="
[[email protected] mydocker]# 

创建父镜像

[[email protected] mydocker]# docker build -f /mydocker/Dockerfile4 -t myip_father .
Sending build context to Docker daemon  5.12 kB
Sending build context to Docker daemon 
Step 0 : FROM centos
 ---> ee2526f4865b
Step 1 : RUN yum install -y curl
 ---> Using cache
 ---> 051811f81190
Step 2 : ENTRYPOINT curl -s https://ip.cn
 ---> Using cache
 ---> 68239c983875
Step 3 : ONBUILD run echo "father images onbuild=================="
 ---> Running in eddfdc5cb940
 ---> 829b19a5ffaa
Removing intermediate container eddfdc5cb940
Successfully built 829b19a5ffaa
[[email protected] mydocker]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
myip_father               latest              829b19a5ffaa        21 seconds ago      285.5 MB
myip2                     latest              68239c983875        35 minutes ago      285.5 MB
myips                     latest              ffb51f2ac6f6        54 minutes ago      285.5 MB
myip                      latest              ffb51f2ac6f6        54 minutes ago      285.5 MB
<none>                    <none>              f04683ff472a        About an hour ago   285.5 MB
mycentos                  1.0                 d200a3402f0f        About an hour ago   385.7 MB
boxiaoyuan/centos         latest              9f3d5ff4c0b1        2 hours ago         201.8 MB
boxiaoyuan/nodocstomcat   1.0                 002a5c87a638        3 hours ago         510.4 MB
tomcat                    latest              d06128b13608        2 days ago          510.3 MB
ubuntu                    latest              d351d647d655        2 weeks ago         64.18 MB
ubuntu                    14.04               fe62d34455b7        7 weeks ago         188.2 MB
centos                    6                   4f2ed42dccff        3 months ago        193.9 MB
centos                    latest              ee2526f4865b        3 months ago        201.8 MB
hello-world               latest              9f5834b25059        6 months ago        1.84 kB
[[email protected] mydocker]# 

创建子镜像DockerFile

[[email protected] mydocker]# cp Dockerfile3 Dockerfile5
[[email protected] mydocker]# vim Dockerfile5
[[email protected] mydocker]# cat Dockerfile5
FROM centos
FROM myip_father
RUN yum install -y curl
CMD ["curl","-s","https://ip.cn"]

创建子镜像

可以看到 # Executing 1 build trigger 执行了一个触发器

[[email protected] mydocker]# docker build -f /mydocker/Dockerfile5 -t myip_son .
Sending build context to Docker daemon 6.144 kB
Sending build context to Docker daemon 
Step 0 : FROM centos
 ---> ee2526f4865b
Step 1 : FROM myip_father
# Executing 1 build triggers
Trigger 0, RUN echo "father images onbuild=================="
Step 0 : RUN echo "father images onbuild=================="
 ---> Running in e7a903d5951c
father images onbuild==================
 ---> f1039405d4c2
Removing intermediate container e7a903d5951c
Step 2 : RUN yum install -y curl
 ---> Running in 6cdaba6781f4
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * extras: mirror.bit.edu.cn
 * updates: mirrors.aliyun.com
Package curl-7.29.0-51.el7.x86_64 already installed and latest version
Nothing to do
 ---> c4e4697711a9
Removing intermediate container 6cdaba6781f4
Step 3 : CMD curl -s https://ip.cn
 ---> Running in d4744a3dc314
 ---> d3de302f3e84
Removing intermediate container d4744a3dc314
Successfully built d3de302f3e84
[[email protected] mydocker]# 

7.3.4 DockerFile案例4自定义镜像 Tomcat 9

ADD 命令:

拷贝+解压缩,将宿主机目录下的文件拷贝进镜像,且ADD命令会自动处理URL和解压tar压缩包

CASE:下面的案例表示将原生的tomcat.tar.gz、jdk.tar.gz拷贝到镜像中并自动解压

COPY 命令:

类似ADD,拷贝文件和目录到镜像中。

将从构建上下文目录中<源文件>的文件/目录复制到新的一层的镜像内的<目录路径>位置

CASE:下面的案例表示将宿主机上的 copy.txt 拷贝到镜像的某个路径下

首先下载apache-tomcat-9.0.12.tar.gz、jdk-8u181-linux-x64.tar.gz 并上传到CentOS

[[email protected] ~]# mkdir -p /boxiaoyuan/dockerfile/tomcat9
[[email protected] ~]# cd /boxiaoyuan/dockerfile/tomcat9/
[[email protected] tomcat9]# pwd
/boxiaoyuan/dockerfile/tomcat9

创建copy.txt、拷贝tomcat、jdk

[[email protected] tomcat9]# touch copy.txt
[[email protected] tomcat9]# cp /root/apache-tomcat-9.0.12.tar.gz .
[[email protected] tomcat9]# cp /root/jdk-8u181-linux-x64.tar.gz .
[[email protected] tomcat9]# ll
总用量 190984
-rw-r--r--. 1 root root   9912675 7月   7 02:21 apache-tomcat-9.0.12.tar.gz
-rw-r--r--. 1 root root         0 7月   7 02:20 copy.txt
-rw-r--r--. 1 root root 185646832 7月   7 02:21 jdk-8u181-linux-x64.tar.gz

DockerFile

[[email protected] tomcat9]# vim Dockerfile 
[[email protected] tomcat9]# cat Dockerfile 
FROM centos
MAINTAINER boxiaoyuan<[email protected]>
# 把宿主机当前的copy.txt拷贝到镜像/usr/local路径下
COPY copy.txt /usr/local/copyincontainer.txt
# 把jdk和tomcat添加到镜像中
ADD jdk-8u181-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.12.tar.gz /usr/local/
# 安装vim
RUN yum -y install vim
# 设置工作访问时的WORKDIR路径,登录落脚点
ENV MYPATH /usr/local
WORKDIR $MYPATH
# 配置jdk、tomcat环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_181
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.12
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.12
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
# 容器运行时监听的端口
EXPOSE 8080
# 启动时运行tomcat
CMD /usr/local/apache-tomcat-9.0.12/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.12/bin/logs/catalina.out
[[email protected] tomcat9]# ll
总用量 190988
-rw-r--r--. 1 root root 9912675 7月 7 02:21 apache-tomcat-9.0.12.tar.gz
-rw-r--r--. 1 root root 0 7月 7 02:20 copy.txt
-rw-r--r--. 1 root root 901 7月 7 02:37 Dockerfile
-rw-r--r--. 1 root root 185646832 7月 7 02:21 jdk-8u181-linux-x64.tar.gz

创建镜像

[[email protected] tomcat9]# docker build -t boxiaoyuantomcat9 .

Sending build context to Docker daemon  195.6MB

……中间省略安装过程                           

Successfully tagged boxiaoyauntomcat9:latest

创建成功

[[email protected] tomcat9]# docker images
REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
boxiaoyuantomcat9         latest              9e3cb2e16ea6        4 minutes ago       757.9 MB
myip_son                  latest              d3de302f3e84        2 hours ago         287.3 MB
myip_father               latest              829b19a5ffaa        2 hours ago         285.5 MB
myip2                     latest              68239c983875        3 hours ago         285.5 MB
myip                      latest              ffb51f2ac6f6        3 hours ago         285.5 MB
myips                     latest              ffb51f2ac6f6        3 hours ago         285.5 MB
<none>                    <none>              f04683ff472a        4 hours ago         285.5 MB
mycentos                  1.0                 d200a3402f0f        4 hours ago         385.7 MB
boxiaoyuan/centos         latest              9f3d5ff4c0b1        5 hours ago         201.8 MB
boxiaoyuan/nodocstomcat   1.0                 002a5c87a638        6 hours ago         510.4 MB
tomcat                    latest              d06128b13608        2 days ago          510.3 MB
ubuntu                    latest              d351d647d655        2 weeks ago         64.18 MB
ubuntu                    14.04               fe62d34455b7        7 weeks ago         188.2 MB
centos                    6                   4f2ed42dccff        3 months ago        193.9 MB
centos                    latest              ee2526f4865b        3 months ago        201.8 MB
hello-world               latest              9f5834b25059        6 months ago        1.84 kB

运行容器

[[email protected] tomcat9]# docker run -d -p 9080:8080 --name mytomcat9 -v /boxiaoyuan/mydockerfile/tomcat9/test:/usr/local/apache-tomcat-9.0.12/webapps/test -v /boxiaoyuan/mydockerfile/tomcat9/tomcat9logs:/usr/local/apache-tomcat-9.0.12/logs --privileged=true boxiaoyuantomcat9
6654d40fd2f0ee4e6820b66e11040727766b9b20378debeb3d70477992b576c1
[[email protected] tomcat9]# docker ps
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS              PORTS                    NAMES
6654d40fd2f0        boxiaoyuantomcat9   "/bin/sh -c ‘/usr/lo   5 seconds ago       Up 4 seconds        0.0.0.0:9080->8080/tcp   mytomcat9           
[[email protected] tomcat9]# docker exec 6654d40fd2f0 ls -l
total 48
drwxr-xr-x. 9 root root 4096 Jul  6 19:01 apache-tomcat-9.0.12
drwxr-xr-x. 2 root root 4096 Apr 11  2018 bin
-rw-r--r--. 1 root root    0 Jul  6 18:20 copyincontainer.txt
drwxr-xr-x. 2 root root 4096 Apr 11  2018 etc
drwxr-xr-x. 2 root root 4096 Apr 11  2018 games
drwxr-xr-x. 2 root root 4096 Apr 11  2018 include
drwxr-xr-x. 7   10  143 4096 Jul  7  2018 jdk1.8.0_181
drwxr-xr-x. 2 root root 4096 Apr 11  2018 lib
drwxr-xr-x. 2 root root 4096 Apr 11  2018 lib64
drwxr-xr-x. 2 root root 4096 Apr 11  2018 libexec
drwxr-xr-x. 2 root root 4096 Apr 11  2018 sbin
drwxr-xr-x. 5 root root 4096 Mar  5 17:34 share
drwxr-xr-x. 2 root root 4096 Apr 11  2018 src

访问 http://192.168.13.145:9080/ 即可成功

在宿主机下创建html文件

[[email protected] test]# pwd
/boxiaoyuan/mydockerfile/tomcat9/test
[[email protected] test]# vim idCard.html 
[[email protected] test]# cat idCard.html 
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
<!-- <input type="text" id="idCard"> -->
<input type="text" name="" id="phone" onblur="phone2(this.value)">
</body>
<script>
function phone2(phone)
     if(!(/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d8$/.test(phone)))
                 console.log(phone+"手机号码有误,请重填");
                alert(phone+"手机号码有误,请重填");
        return false;
    

</script>
</html>
[[email protected] test]# 

因为数据卷的共享性,在宿主机有关联的文件下进行增删改,相应的容器内会同步过去,也就是说以后部署项目可以直接在宿主机上发布即可。

访问 http://192.168.13.145:9080/test/idCard.html 可进行测试。

以上是关于九Dockerfile的主要内容,如果未能解决你的问题,请参考以下文章

Docker重学系列之Dockerfile

Docker 从入门到进阶七:DockerFile 与 Docker Compose

用for循环编程打印九九乘法表

怎么用java编写程序实现九九乘法表?

编写一个JSP页面,输出九九乘法表

怎么用python写一个九九乘法表?