Docker 可以通过 Dockerfile 的内容来自动构建镜像。Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本,通过docker build命令可以根据 Dockerfile 的内容构建镜像。
(1)每条保留字指令都必须为大写字母且后面跟至少一个参数
(2)指令按照从上到下,顺序执行
(3)#表示注释
(4)每条指令都会创建一个新的镜像层,并对镜像层进行提交
(1)docker从基础镜像运行一个容器
(2)执行一条指令并对容器作出修改
(3)执行类似docker commit的操作提交一个新的镜像层
(4)docker再基于刚提交的镜像运行一个新容器
(5)执行dockerfile中的下一条指令直到所有指令都执行完成
从应用软件的角度来看,Dockerfile、Docker和Docker容器分别代表软件的三个不同阶段:
Dockerfile是软件的原材料
Docker镜像是软件的交付品
Docker容器是软件的运行态
Dockerfile面向开发,Docker镜像成为交付标准,Docker容器则涉及部署与运维,三者缺一不可,合力充当Docker体系的基础。
FROM
FROM指定构建镜像的基础源镜像,如果本地没有指定的镜像,则会自动从 Docker 的公共库 pull 镜像下来。
FROM必须是 Dockerfile 中非注释行的第一个指令,即一个 Dockerfile 从FROM语句开始。
FROM可以在一个 Dockerfile 中出现多次,如果有需求在一个 Dockerfile 中创建多个镜像。
如果FROM语句没有指定镜像标签,则默认使用latest标签。
MAINTAINER
镜像维护者的姓名和邮箱地址
容器构建时需要运行的命令
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,如此便不会缓存。
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无关了。
EXPOSE
告诉 Docker 服务端容器对外映射的本地端口,需要在 docker run 的时候使用-p或者-P选项生效。
ENV
ENV
指定一个环境变量,会被后续RUN指令使用,并在容器运行时保留。
ADD
将宿主机目录下的文件拷贝进镜像且ADD命令会自动处理URL和解压tar压缩包
路径必须是绝对路径,如果不存在,会自动创建对应目录
路径必须是 Dockerfile 所在路径的相对路径
如果是一个目录,只会复制目录下的内容,而目录本身则不会被复制
COPY
类似ADD,拷贝文件和目录到镜像中,将从构建上下文目录中<源路径>的文件/目录复制到新的一层的镜像内的<目标路径>位置
ENTRYPOINT "executable", "param1", "param2"
ENTRYPOINT command param1 param2 (shell form)
配置容器启动后执行的命令,并且不可被 docker run 提供的参数覆盖,而CMD是可以被覆盖的。如果需要覆盖,则可以使用docker run --entrypoint选项。
每个 Dockerfile 中只能有一个ENTRYPOINT,当指定多个时,只有最后一个生效。
VOLUME ["/data"]
容器数据卷,用于数据保存和持久化工作
WORKDIR /path/to/workdir
指定在创建容器后,终端默认登录进来的工作目录,一个落脚点。
ONBUILD [INSTRUCTION]
当构建一个被继承的Dockerfile时运行命令,父镜像在被子继承后父镜像的onbuild被触发。
Docker Hub中99%的镜像都是通过base镜像中安装和配置需要的软件构建出来的。
Docker Hub默认CentOS镜像什么情况
[root@boxiaoyuan ~]# 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 [root@boxiaoyuan ~]# docker rm -f $(docker ps -q) 25501c70bdf8 [root@boxiaoyuan ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@boxiaoyuan ~]# docker run -it centos /bin/bash [root@341fa0e6e8d7 /]# pwd / [root@341fa0e6e8d7 /]# vim a.txt bash: vim: command not found [root@341fa0e6e8d7 /]# ifconfig bash: ifconfig: command not found [root@341fa0e6e8d7 /]#
可以发现,Docker Hub的CentOS为何只有200MB,而宿主机的CentOS有4个G,Docker Hub上的是去除了很多不需要的东西,只保留了重要的内核文件的一个精简版的CentOS。
因此,我们来写个自定义的CentOS镜像,具备如下:
登陆后的默认路径
vim编辑器
查看网络配置ifconfig支持
创建Dockerfile2
[root@boxiaoyuan mydocker]# vim Dockerfile2 [root@boxiaoyuan mydocker]# cat Dockerfile2 FROM centos MAINTAINER boxiaoyuanENV 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 [root@boxiaoyuan mydocker]#
创建镜像
不要忘记最后的 .
[root@boxiaoyuan 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---> 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
生成镜像完毕
[root@boxiaoyuan 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
运行镜像
[root@boxiaoyuan mydocker]# docker run -it mycentos:1.0 [root@2123a25ea8c3 local]# pwd /usr/local [root@2123a25ea8c3 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 [root@2123a25ea8c3 local]# vim test.txt [root@2123a25ea8c3 local]# cat test.txt test [root@2123a25ea8c3 local]# ifconfig eth0: flags=4163mtu 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 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 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 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/镜像名称
[root@boxiaoyuan 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 [root@boxiaoyuan 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 boxiaoyuan0 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 [root@boxiaoyuan mydocker]#
创建查询ip的镜像
[root@boxiaoyuan 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 [root@boxiaoyuan mydocker]# vim Dockerfile3 [root@boxiaoyuan mydocker]# cat Dockerfile3 FROM centos RUN yum install -y curl CMD ["curl","-s","https://ip.cn"] [root@boxiaoyuan 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
运行容器
[root@boxiaoyuan 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 MBf04683ff472a 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 [root@boxiaoyuan mydocker]# docker run -it myip {"ip": "112.223.21.129", "country": "山东省济南市", "city": "联通"}
在宿主机终端执行curl -s -i http://ip.cn
[root@boxiaoyuan mydocker]# curl -s https://ip.cn {"ip": "112.224.22.119", "country": "山东省济南市", "city": "联通"} [root@boxiaoyuan 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": "联通"} [root@boxiaoyuan mydocker]#
在容器终端执行
[root@boxiaoyuan 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信息的容器
创建镜像
[root@boxiaoyuan mydocker]# vim Dockerfile4 [root@boxiaoyuan mydocker]# cat Dockerfile4 FROM centos RUN yum install -y curl ENTRYPOINT ["curl","-s","https://ip.cn"] [root@boxiaoyuan 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
运行容器
[root@boxiaoyuan mydocker]# docker run myip {"ip": "112.224.22.119", "country": "山东省济南市", "city": "联通"}
对比下CMD 和 ENTRYPOINT,就可以很清楚的区分两个命令的不同点:前者会被新加的命令覆盖,后者则是在原有的命令后加上新的命令
[root@boxiaoyuan mydocker]# docker run myip2 {"ip": "112.224.22.119", "country": "山东省济南市", "city": "联通"} [root@boxiaoyuan 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": "联通"} [root@boxiaoyuan mydocker]#
类似触发器,当构建一个被继承的DockerFile时运行命令,父镜像在被子镜像继承后父镜像的ONBUILD被触发。
[root@boxiaoyuan 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 [root@boxiaoyuan mydocker]# vim Dockerfile4 [root@boxiaoyuan mydocker]# cat Dockerfile4 FROM centos RUN yum install -y curl ENTRYPOINT ["curl","-s","https://ip.cn"] ONBUILD RUN echo "father images Onbuild==================" [root@boxiaoyuan mydocker]#
创建父镜像
[root@boxiaoyuan 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 [root@boxiaoyuan 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 MBf04683ff472a 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 [root@boxiaoyuan mydocker]#
创建子镜像DockerFile
[root@boxiaoyuan mydocker]# cp Dockerfile3 Dockerfile5 [root@boxiaoyuan mydocker]# vim Dockerfile5 [root@boxiaoyuan mydocker]# cat Dockerfile5 FROM centos FROM myip_father RUN yum install -y curl CMD ["curl","-s","https://ip.cn"]
创建子镜像
可以看到 # Executing 1 build trigger 执行了一个触发器
[root@boxiaoyuan 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 [root@boxiaoyuan mydocker]#
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 上
[root@boxiaoyuan ~]# mkdir -p /boxiaoyuan/dockerfile/tomcat9 [root@boxiaoyuan ~]# cd /boxiaoyuan/dockerfile/tomcat9/ [root@boxiaoyuan tomcat9]# pwd /boxiaoyuan/dockerfile/tomcat9
创建copy.txt、拷贝tomcat、jdk
[root@boxiaoyuan tomcat9]# touch copy.txt [root@boxiaoyuan tomcat9]# cp /root/apache-tomcat-9.0.12.tar.gz . [root@boxiaoyuan tomcat9]# cp /root/jdk-8u181-linux-x64.tar.gz . [root@boxiaoyuan 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
[root@boxiaoyuan tomcat9]# vim Dockerfile [root@boxiaoyuan tomcat9]# cat Dockerfile FROM centos MAINTAINER boxiaoyuan# 把宿主机当前的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
[root@boxiaoyuan 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
创建镜像
[root@boxiaoyuan tomcat9]# docker build -t boxiaoyuantomcat9 . Sending build context to Docker daemon 195.6MB ……中间省略安装过程 Successfully tagged boxiaoyauntomcat9:latest
创建成功
[root@boxiaoyuan 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 MBf04683ff472a 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
运行容器
[root@boxiaoyuan 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 [root@boxiaoyuan 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 [root@boxiaoyuan 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文件
[root@boxiaoyuan test]# pwd /boxiaoyuan/mydockerfile/tomcat9/test [root@boxiaoyuan test]# vim idCard.html [root@boxiaoyuan test]# cat idCard.html "en"> "UTF-8"> "text" name="" id="phone" Onblur="phone2(this.value)"> [root@boxiaoyuan test]#
因为数据卷的共享性,在宿主机有关联的文件下进行增删改,相应的容器内会同步过去,也就是说以后部署项目可以直接在宿主机上发布即可。
访问 http://192.168.13.145:9080/test/idCard.html 可进行测试。
九、Dockerfile