欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker_周阳(四)

程序员文章站 2022-04-14 13:41:01
...

DockerFile 解析

是什么

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

构建三步骤

  1. 编写Dockerfile文件
  2. docker build
  3. docker run

DockerFile构建过程解析

Dockerfile内容基础知识

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

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

3:#表示注释

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

Docker执行Dockerfile的大致流程

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

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

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

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

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

小总结

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

* Dockerfile是软件的原材料

* Docker镜像是软件的交付品

* Docker容器则可以认为是软件的运行态。

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

docker_周阳(四)

  1. Dockerfile:需要定义一个Dockerfile,Dockerfile定义了进程需要的一切东西。Dockerfile涉及的内容包括执行代码或者是文件、环境变量、依赖包、运行时环境、动态链接库、操作系统的发行版、服务进程和内核进程(当应用进程需要和系统服务和内核进程打交道,这时需要考虑如何设计namespace的权限控制)等等;
  2. Docker镜像:在用Dockerfile定义一个文件之后,docker build时会产生一个Docker镜像,当运行 Docker镜像时,会真正开始提供服务;
  3. Docker容器:容器是直接提供服务的。

DockerFile体系结构(保留字指令)

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

小总结

docker_周阳(四)

案例

Base镜像(scratch)

Docker Hub 中 99% 的镜像都是通过在 base 镜像中安装和配置需要的软件构建出来的(基类object)

自定义镜像mycentos

自定义mycentos目的使我们自己的镜像具备如下:

  • 登陆后的默认路径

  • vim编辑器

  • 查看网络配置ifconfig支持

编写
[aaa@qq.com /]# cd mydocker/
[aaa@qq.com mydocker]# ls
dockerfile01
[aaa@qq.com mydocker]# vim dockerfile02
dockerfile02的内容
FROM centos
MAINTAINER luke<aaa@qq.com>

ENV MYPATH /tmp
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
构建
docker build -t 新镜像名字:TAG .
[aaa@qq.com mydocker]# docker build -f /mydocker/dockerfile02 -t mycentos:1.3 .
Sending build context to Docker daemon  3.072kB
Step 1/10 : FROM centos
 ---> 0f3e07c0138f
Step 2/10 : MAINTAINER luke<aaa@qq.com>
 ---> Running in e2701e3e38a4
Removing intermediate container e2701e3e38a4
 ---> fef748f73374
Step 3/10 : ENV MYPATH /tmp
 ---> Running in 7865d6258263
Removing intermediate container 7865d6258263
 ---> 022513486ddd
Step 4/10 : WORKDIR $MYPATH
 ---> Running in 16a6795f3eb8
Removing intermediate container 16a6795f3eb8
 ---> f452a438125f
Step 5/10 : RUN yum -y install vim
 ---> Running in 50386caf9113
CentOS-8 - AppStream                            1.9 MB/s | 6.3 MB     00:03    
CentOS-8 - Base                                 1.3 MB/s | 7.9 MB     00:06    
CentOS-8 - Extras                               772  B/s | 2.1 kB     00:02    
Dependencies resolved.
================================================================================
 Package             Arch        Version                   Repository      Size
================================================================================
Installing:
 vim-enhanced        x86_64      2:8.0.1763-10.el8         AppStream      1.4 M

...

Installed:
  net-tools-2.0-0.51.20160912git.el8.x86_64                                     

Complete!
Removing intermediate container f8e3672891cb
 ---> a083352fef95
Step 7/10 : EXPOSE 80
 ---> Running in 4aaf338c19e3
Removing intermediate container 4aaf338c19e3
 ---> 7904cd2f61d5
Step 8/10 : CMD echo $MYPATH
 ---> Running in 8d8118bc2bd0
Removing intermediate container 8d8118bc2bd0
 ---> 5b5095b5935a
Step 9/10 : CMD echo "success-----------ok"
 ---> Running in 4594aec42c76
Removing intermediate container 4594aec42c76
 ---> 6f6b4b33fd3e
Step 10/10 : CMD /bin/bash
 ---> Running in 5d33f94b112e
Removing intermediate container 5d33f94b112e
 ---> d538b2b0fae7
Successfully built d538b2b0fae7
Successfully tagged mycentos:1.3
查看
[aaa@qq.com mydocker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mycentos            1.3                 d538b2b0fae7        50 seconds ago      316MB
luke/centos         latest              5743072abd8f        2 days ago          220MB
luke2/centos        latest              5743072abd8f        2 days ago          220MB
luke/mytomcat       2.0                 ec4a01b95ae1        3 days ago          507MB
mongo               latest              a0e2e64ac939        8 days ago          364MB
tomcat              latest              6fa48e047721        2 weeks ago         507MB
centos              latest              0f3e07c0138f        2 months ago        220MB
hello-world         latest              fce289e99eb9        12 months ago       1.84kB
检测
[aaa@qq.com mydocker]# docker run -it mycentos:1.3
[aaa@qq.com tmp]# pwd
/tmp
[aaa@qq.com tmp]# vim a.txt
[aaa@qq.com tmp]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 8  bytes 656 (656.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

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (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
列出镜像的变更历史
[aaa@qq.com ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mycentos            1.3                 d538b2b0fae7        45 hours ago        316MB
luke/centos         latest              5743072abd8f        4 days ago          220MB
luke2/centos        latest              5743072abd8f        4 days ago          220MB
luke/mytomcat       2.0                 ec4a01b95ae1        5 days ago          507MB
mongo               latest              a0e2e64ac939        10 days ago         364MB
tomcat              latest              6fa48e047721        2 weeks ago         507MB
centos              latest              0f3e07c0138f        2 months ago        220MB
hello-world         latest              fce289e99eb9        12 months ago       1.84kB
[aaa@qq.com ~]# docker history d538b2b0fae7
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d538b2b0fae7        45 hours ago        /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "/bin…   0B                  
6f6b4b33fd3e        45 hours ago        /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B                  
5b5095b5935a        45 hours ago        /bin/sh -c #(nop)  CMD ["/bin/sh" "-c" "echo…   0B                  
7904cd2f61d5        45 hours ago        /bin/sh -c #(nop)  EXPOSE 80                    0B                  
a083352fef95        45 hours ago        /bin/sh -c yum -y install net-tools             29.3MB              
ccddaa164196        45 hours ago        /bin/sh -c yum -y install vim                   66.9MB              
f452a438125f        45 hours ago        /bin/sh -c #(nop) WORKDIR /tmp                  0B                  
022513486ddd        45 hours ago        /bin/sh -c #(nop)  ENV MYPATH=/tmp              0B                  
fef748f73374        45 hours ago        /bin/sh -c #(nop)  MAINTAINER luke<luke12345…   0B                  
0f3e07c0138f        2 months ago        /bin/sh -c #(nop)  CMD ["/bin/bash"]            0B                  
<missing>           2 months ago        /bin/sh -c #(nop)  LABEL org.label-schema.sc…   0B                  
<missing>           2 months ago        /bin/sh -c #(nop) ADD file:d6fdacc1972df524a…   220MB               

CMD/ENTRYPOINT 镜像案例

都是指定一个容器启动时要运行的命令

CMD

Dockerfile 中可以有多个CMD 指令,但只有最后一个生效,CMD 会被 docker run 之后的参数替换

[aaa@qq.com ~]# cd /mydocker/
[aaa@qq.com mydocker]# vim dockerfile03
FROM centos
RUN yum install -y curl
CMD ["curl","-s","http://ip.cn"]
[aaa@qq.com mydocker]# docker build -f /mydocker/dockerfile03 -t myip .
...
[aaa@qq.com mydocker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myip                latest              488b1bdedbb3        About an hour ago   249MB
mycentos            1.3                 d538b2b0fae7        2 days ago          316MB
luke/centos         latest              5743072abd8f        4 days ago          220MB
luke2/centos        latest              5743072abd8f        4 days ago          220MB
luke/mytomcat       2.0                 ec4a01b95ae1        5 days ago          507MB
mongo               latest              a0e2e64ac939        10 days ago         364MB
tomcat              latest              6fa48e047721        2 weeks ago         507MB
centos              latest              0f3e07c0138f        2 months ago        220MB
hello-world         latest              fce289e99eb9        12 months ago       1.84kB

[aaa@qq.com mydocker]# docker run myip
[aaa@qq.com mydocker]# 
[aaa@qq.com mydocker]# docker run myip -i
docker: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused "exec: \"-i\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled 
ENTRYPOINT
[aaa@qq.com mydocker]# vim dockerfile04
FROM centos
RUN yum install -y curl
ENTRYPOINT ["curl","-s","http://ip.cn"]
[aaa@qq.com mydocker]# docker build -f /mydocker/dockerfile04 -t myip2 .
...
[aaa@qq.com mydocker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myip2               latest              e10ba8b6458e        12 minutes ago      249MB
myip                latest              488b1bdedbb3        About an hour ago   249MB
mycentos            1.3                 d538b2b0fae7        2 days ago          316MB
luke/centos         latest              5743072abd8f        4 days ago          220MB
luke2/centos        latest              5743072abd8f        4 days ago          220MB
luke/mytomcat       2.0                 ec4a01b95ae1        5 days ago          507MB
mongo               latest              a0e2e64ac939        10 days ago         364MB
tomcat              latest              6fa48e047721        2 weeks ago         507MB
centos              latest              0f3e07c0138f        2 months ago        220MB
hello-world         latest              fce289e99eb9        12 months ago       1.84kB
[aaa@qq.com mydocker]# docker run myip2 -i
HTTP/1.1 301 Moved Permanently
Date: Mon, 30 Dec 2019 03:58:26 GMT
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: max-age=3600
Expires: Mon, 30 Dec 2019 04:58:26 GMT
Location: https://ip.cn/
Server: cloudflare
CF-RAY: 54d118689d4492f2-SJC

onbuild

[aaa@qq.com mydocker]# vim dockerfile05
FROM centos
RUN yum install -y curl
ENTRYPOINT ["curl","-s","http://ip.cn"]
ONBUILD RUN echo "father images onbuild ------ 886"
[aaa@qq.com mydocker]# docker build -f /mydocker/dockerfile05 -t myip_father .
Sending build context to Docker daemon  6.144kB
Step 1/4 : FROM centos
 ---> 0f3e07c0138f
Step 2/4 : RUN yum install -y curl
 ---> Using cache
 ---> 5a06a457e691
Step 3/4 : ENTRYPOINT ["curl","-s","http://ip.cn"]
 ---> Using cache
 ---> e10ba8b6458e
Step 4/4 : ONBUILD RUN echo "father images onbuild ------ 886"
 ---> Running in cc0e5408a816
Removing intermediate container cc0e5408a816
 ---> 28e88da635ca
Successfully built 28e88da635ca
Successfully tagged myip_father:latest

[aaa@qq.com mydocker]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
myip_father         latest              28e88da635ca        4 minutes ago       249MB
myip2               latest              e10ba8b6458e        2 hours ago         249MB
myip                latest              488b1bdedbb3        4 hours ago         249MB
...
[aaa@qq.com mydocker]# cp dockerfile03 dockerfile055
[aaa@qq.com mydocker]# vim dockerfile055
[aaa@qq.com mydocker]# cat dockerfile055
FROM myip_father
RUN yum install -y curl
CMD ["curl","-s","http://ip.cn"]

[aaa@qq.com mydocker]# docker build -f /mydocker/dockerfile055 -t myip_son .
Sending build context to Docker daemon  7.168kB
Step 1/3 : FROM myip_father
# Executing 1 build trigger
 ---> Running in 001cbe836261
father images onbuild ------ 886
Removing intermediate container 001cbe836261
 ---> 7ed3c1317549
Step 2/3 : RUN yum install -y curl
 ---> Running in 3f8c773d1f30
Last metadata expiration check: 3:39:45 ago on Mon Dec 30 02:26:52 2019.
Package curl-7.61.1-8.el8.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
Removing intermediate container 3f8c773d1f30
 ---> 62fe079a4f53
Step 3/3 : CMD ["curl","-s","http://ip.cn"]
 ---> Running in d13082f5daef
Removing intermediate container d13082f5daef
 ---> ede0f2020686
Successfully built ede0f2020686
Successfully tagged myip_son:latest

自定义镜像Tomcat9

[aaa@qq.com mydocker]# mkdir tomcat9
[aaa@qq.com mydocker]# cd tomcat9/
[aaa@qq.com tomcat9]# touch c.txt
[aaa@qq.com tomcat9]# ls
c.txt

jdktomcat安装的压缩包拷贝到tomcat9文件夹中

[aaa@qq.com tomcat9]# ls
apache-tomcat-9.0.30.tar.gz  c.txt  jdk-8u11-linux-x64.tar.gz
新建Dockerfile文件
[aaa@qq.com tomcat9]# vim Dockerfile
FROM centos
MAINTAINER luke<aaa@qq.com>
# 把宿主机当前上下文的c.txt拷贝到容器/usr/local/路径下
COPY c.txt /usr/local/cincontainer.txt
# 把Java与tomcat添加到容器中
ADD jdk-8u11-linux-x64.tar.gz /usr/local/
ADD apache-tomcat-9.0.30.tar.gz /usr/local/
# 安装vim编辑器
RUN yum -y install vim
# 设置工作访问时候的WORKDIR路径,登录落脚点
ENV MYPATH /usr/local
WORKDIR $MYPATH

# 配置Java与tomcat的环境变量
ENV JAVA_HOME /usr/local/jdk1.8.0_11
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.30
ENV CARALINA_BASE /usr/local/apache-tomcat-9.0.30
ENV PATH $PATH:$JAVA_HOME:$CATALINA_HOME/lib:$CATALINA_HOME/bin

# 容器运行时监听的端口
EXPOSE 8080

# 启动时运行tomcat
# ENTRYPOINT ["/usr/local/apache-tomcat-9.0.30/bin/startup.sh"]
# CMD ["/usr/local/apache-tomcat-9.0.30/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-9.0.30/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.30/bin/logs/catalina.out

目录内容:

[aaa@qq.com tomcat9]# ls
apache-tomcat-9.0.30.tar.gz  c.txt  Dockerfile  jdk-8u11-linux-x64.tar.gz
构建
[aaa@qq.com tomcat9]# docker build -t luketomcat9 .
Sending build context to Docker daemon  170.1MB
Step 1/15 : FROM centos
 ---> 0f3e07c0138f
Step 2/15 : MAINTAINER luke<aaa@qq.com>
 ---> Running in 97b81006d218
Removing intermediate container 97b81006d218
 ---> bfa480ed48e0
Step 3/15 : COPY c.txt /usr/local/cincontainer.txt
 ---> 278191785a6b
Step 4/15 : ADD jdk-8u11-linux-x64.tar.gz /usr/local/
 ---> e4c51f921837
Step 5/15 : ADD apache-tomcat-9.0.30.tar.gz /usr/local/
 ---> d3d2e8967898
Step 6/15 : RUN yum -y install vim
 ---> Running in 64c49c4a2b02
CentOS-8 - AppStream                            1.4 MB/s | 6.3 MB     00:04    
...
Installed:
  vim-enhanced-2:8.0.1763-10.el8.x86_64 gpm-libs-1.20.7-15.el8.x86_64          
  vim-common-2:8.0.1763-10.el8.x86_64   vim-filesystem-2:8.0.1763-10.el8.noarch
  which-2.21-10.el8.x86_64             

Complete!
Removing intermediate container 64c49c4a2b02
 ---> ced2952e172b
Step 7/15 : ENV MYPATH /usr/local
 ---> Running in 3a8ff7839057
Removing intermediate container 3a8ff7839057
 ---> 8bb130605b9d
Step 8/15 : WORKDIR $MYPATH
 ---> Running in 6bfaaabac30b
Removing intermediate container 6bfaaabac30b
 ---> 8c77a4de4093
Step 9/15 : ENV JAVA_HOME /usr/local/jdk1.8.0_11
 ---> Running in eccd6dc67459
Removing intermediate container eccd6dc67459
 ---> 6f23b646f280
Step 10/15 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in 609e0c0cb6c2
Removing intermediate container 609e0c0cb6c2
 ---> c9900838189a
Step 11/15 : ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.30
 ---> Running in 5fa7c1c566ed
Removing intermediate container 5fa7c1c566ed
 ---> 3eb7d993ea81
Step 12/15 : ENV CARALINA_BASE /usr/local/apache-tomcat-9.0.30
 ---> Running in 9cb033fdfadc
Removing intermediate container 9cb033fdfadc
 ---> 1eb3140aa0a4
Step 13/15 : ENV PATH $PATH:$JAVA_HOME:$CATALINA_HOME/lib:$CATALINA_HOME/bin
 ---> Running in 4a8c39b57b8a
Removing intermediate container 4a8c39b57b8a
 ---> b71c2dee5e34
Step 14/15 : EXPOSE 8080
 ---> Running in 29bba9b58d78
Removing intermediate container 29bba9b58d78
 ---> 7db5c045b009
Step 15/15 : CMD /usr/local/apache-tomcat-9.0.30/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.30/bin/logs/catalina.out
 ---> Running in 6ccd637f0bce
Removing intermediate container 6ccd637f0bce
 ---> 74064274fc8e
Successfully built 74064274fc8e
Successfully tagged luketomcat9:latest
构建完成
[aaa@qq.com tomcat9]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
luketomcat9         latest              74064274fc8e        11 seconds ago      626MB
myip_son            latest              ede0f2020686        3 days ago          266MB
myip_father         latest              28e88da635ca        3 days ago          249MB
myip2               latest              e10ba8b6458e        3 days ago          249MB
myip                latest              488b1bdedbb3        3 days ago          249MB
mycentos            1.3                 d538b2b0fae7        5 days ago          316MB
luke/centos         latest              5743072abd8f        7 days ago          220MB
luke2/centos        latest              5743072abd8f        7 days ago          220MB
luke/mytomcat       2.0                 ec4a01b95ae1        8 days ago          507MB
mongo               latest              a0e2e64ac939        2 weeks ago         364MB
tomcat              latest              6fa48e047721        2 weeks ago         507MB
centos              latest              0f3e07c0138f        3 months ago        220MB
hello-world         latest              fce289e99eb9        12 months ago       1.84kB
运行
[aaa@qq.com tomcat9]#  docker run -d -p 9080:8080 --name myt9 -v /mydocker/tomcat9/test:/usr/local/apache-tomcat-9.0.30/webapps/test -v /mydocker/tomcat9/tomcatlogs/:/usr/local/apache-tomcat-9.0.30/logs luketomcat9
ef156174d14f93bdee95c5478127db3f098391a341d4717f34a8f26a92d7f452
[aaa@qq.com tomcat9]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
ef156174d14f        luketomcat9         "/bin/sh -c '/usr/lo…"   11 seconds ago      Up 7 seconds        0.0.0.0:9080->8080/tcp   myt9
验证

访问127.0.0.1:9080

[aaa@qq.com tomcat9]# docker exec ef156174d14f ls -l
total 0
drwxr-xr-x. 1 root root  45 Jan  2 09:19 apache-tomcat-9.0.30
drwxr-xr-x. 2 root root   6 May 11  2019 bin
-rw-r--r--. 1 root root   0 Dec 30 11:26 cincontainer.txt
drwxr-xr-x. 2 root root   6 May 11  2019 etc
drwxr-xr-x. 2 root root   6 May 11  2019 games
drwxr-xr-x. 2 root root   6 May 11  2019 include
drwxr-xr-x. 8   10  143 255 Jun 17  2014 jdk1.8.0_11
drwxr-xr-x. 2 root root   6 May 11  2019 lib
drwxr-xr-x. 2 root root   6 May 11  2019 lib64
drwxr-xr-x. 2 root root   6 May 11  2019 libexec
drwxr-xr-x. 2 root root   6 May 11  2019 sbin
drwxr-xr-x. 5 root root  49 Sep 27 17:13 share
drwxr-xr-x. 2 root root   6 May 11  2019 src
[aaa@qq.com tomcat9]# ls
apache-tomcat-9.0.30.tar.gz  c.txt  Dockerfile  jdk-8u11-linux-x64.tar.gz  test  tomcatlogs
测试web服务test容器卷
[aaa@qq.com tomcat9]# cd test
[aaa@qq.com test]# mkdir WEB-INF
[aaa@qq.com test]# cd WEB-INF
[aaa@qq.com WEB-INF]# vim web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  id="WebApp_ID" version="2.5">
  
  <display-name>test</display-name>
 
</web-app>
[aaa@qq.com WEB-INF]# cd ..
[aaa@qq.com test]# vim a.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
  </head>
  <body>
    -----------welcome------------
    <%="i am in docker tomcat self "%>
    <br>
    <br>
    <% System.out.println("=============docker tomcat self");%>
  </body>
</html>
[aaa@qq.com test]# ls
a.jsp  WEB-INF
[aaa@qq.com test]# docker exec ef156174d14f ls -l /usr/local/apache-tomcat-9.0.30/webapps/test
total 4
drwxr-xr-x. 2 root root  21 Jan  2 11:33 WEB-INF
-rw-r--r--. 1 root root 509 Jan  2 11:32 a.jsp
[aaa@qq.com test]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
ef156174d14f        luketomcat9         "/bin/sh -c '/usr/lo…"   15 minutes ago      Up 15 minutes       0.0.0.0:9080->8080/tcp   myt9
[aaa@qq.com test]# docker restart ef156174d14f
ef156174d14f
[aaa@qq.com test]# vim a.jsp 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
  </head>
  <body>
    -----------welcome------------
<br>
<br>
    <%="i am in docker tomcat self 2222"%>
    <br>
    <br>
    <% System.out.println("=============docker tomcat self");%>
  </body>
</html>
[aaa@qq.com test]# cd ..
[aaa@qq.com tomcat9]# ls
apache-tomcat-9.0.30.tar.gz  c.txt  Dockerfile  jdk-8u11-linux-x64.tar.gz  test  tomcatlogs
[aaa@qq.com tomcat9]# cd tomcatlogs/
[aaa@qq.com tomcatlogs]# ll
总用量 48
-rw-r-----. 1 root root 20141 1月   2 20:02 catalina.2020-01-02.log
-rw-r-----. 1 root root 20205 1月   2 20:02 catalina.out
-rw-r-----. 1 root root     0 1月   2 19:21 host-manager.2020-01-02.log
-rw-r-----. 1 root root  1223 1月   2 20:02 localhost.2020-01-02.log
-rw-r-----. 1 root root  1511 1月   2 20:02 localhost_access_log.2020-01-02.txt
-rw-r-----. 1 root root     0 1月   2 19:21 manager.2020-01-02.log

[aaa@qq.com tomcatlogs]# cat catalina.out 
02-Jan-2020 11:21:36.520 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/9.0.30
02-Jan-2020 11:21:36.522 INFO [main] 
...
startup in [1,582] milliseconds
=============docker tomcat self
=============docker tomcat self
=============docker tomcat self

小总结

docker_周阳(四)

相关标签: 容器技术