Docker容器-构建Tomcat业务镜像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker容器-构建Tomcat业务镜像相关的知识,希望对你有一定的参考价值。

接着上一篇文章来构建Tomcat业务镜像,上篇已经把tomcat基础镜像已经完成了。这次基于tomcat基础镜像构建实际业务镜像。

退到tomcat目录下,新建两个业务用的目录文件

[[email protected] /data/dockerfile/web/tomcat/tomcat-base]#cd ..
[[email protected] /data/dockerfile/web/tomcat]#mkdir tomcat-app1
[[email protected] /data/dockerfile/web/tomcat]#mkdir tomcat-app2

进入Tomcat-app1目录下创建Dockerfile文件

[[email protected] /data/dockerfile/web/tomcat]#cd tomcat-app1
[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#vim Dockerfile
FROM 172.20.7.50/baseimages/centos-tomcat:8.5.33
LABEL maintainer "mr.luo <[email protected]>"

RUN mkdir -p /data/tomcat/webapps/myapp && useradd tomcat
ADD index.html  /data/tomcat/webapps/myapp
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
COPY server.xml /apps/tomcat/conf/server.xml
RUN chown -R tomcat.tomcat /apps /data/tomcat/webapps

EXPOSE 8080

CMD ["/apps/tomcat/bin/run_tomcat.sh"]

创建测试文件

[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#vim index.html
<h1>tomcat testapp1</h1>

拷贝server.xml文件到当前目录下。

[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#cp /usr/local/apache-tomcat-8.5.33/conf/server.xml ./

编辑server.xml文件修改host的路径

[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#vim server.xml
<Host name="localhost"  appBase="/data/tomcat/webapps" unpackWARs="true" autoDeploy="true">

创建tomcat启动脚本、并给脚本添加执行权限

[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#vim run_tomcat.sh 
#!/bin/bash
echo "1.1.1.1 abc.test.com" >> /etc/hosts
echo "nameserver 223.5.5.5" > /etc/resolv.conf

su - tomcat -c "/apps/tomcat/bin/catalina.sh start"
su - tomcat -c "tail -f /etc/hosts"

# 给脚本添加执行权限
[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#chmod +x run_tomcat.sh

创建docker build 脚本

[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#vim build-command.sh
#!/bin/bash
# Dockerfile build shell
docker build -t 172.20.7.50/baseimages/centos-tomcat-web:app1  .

开始制作镜像

[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#bash build-command.sh 
Sending build context to Docker daemon  13.31kB
Step 1/9 : FROM 172.20.7.50/baseimages/centos-tomcat:8.5.33
---> 872c70dd2181
Step 2/9 : LABEL maintainer "mr.luo <[email protected]>"
---> Using cache
---> e97895b822f7
Step 3/9 : RUN mkdir -p /data/tomcat/webapps/myapp && useradd tomcat
---> Using cache
---> 72bb5e03ae16
Step 4/9 : ADD index.html  /data/tomcat/webapps/myapp
---> 1f7213ebca35
Step 5/9 : ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
---> 810821a8e3ec
Step 6/9 : COPY server.xml /apps/tomcat/conf/server.xml
---> c0e804cc499d
Step 7/9 : RUN chown -R tomcat.tomcat /apps /data/tomcat/webapps
---> Running in a4a8923b3d14
Removing intermediate container a4a8923b3d14
---> ef1bc1eeba9c
Step 8/9 : EXPOSE 8080
---> Running in f0932e1e1990
Removing intermediate container f0932e1e1990
---> bc4e67b65e5e
Step 9/9 : CMD ["/apps/tomcat/bin/run_tomcat.sh"]
---> Running in cba94e6d84e1
Removing intermediate container cba94e6d84e1
---> 3714c600c51e
Successfully built 3714c600c51e
Successfully tagged 172.20.7.50/baseimages/centos-tomcat-web:app1

启动刚制作好的镜像,测试tomcat是否制作成功

[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#docker run --name tom1 -it -d -p 8801:8080 172.20.7.50/baseimages/centotomcat-web:app1 
6b77dd85d8a74b7475bd3340e501d3bcd0019ffdad2ea0505ca371f52c3d2ae6

查看是否在后台正常运行

[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]#docker ps 
CONTAINER ID        IMAGE                                           COMMAND                  CREATED             STATUS              PORTS                    NAMES
6b77dd85d8a7        172.20.7.50/baseimages/centos-tomcat-web:app1   "/apps/tomcat/bin/ru…"   6 seconds ago       Up 5 seconds        0.0.0.0:8801->8080/tcp   tom1

用物理机的浏览器测试

技术分享图片

制作第二个就非常简单了只需要在第一个上面改下名称即可

先拷贝tomcat-app1下所有文件到tomcat-app2下即可

[[email protected] /data/dockerfile/web/tomcat/tomcat-app1]# cd ..
[[email protected] /data/dockerfile/web/tomcat]#cp -rf tomcat-app1/* tomcat-app2/

修改index.html标识为tomcat testapp2即可

[[email protected] /data/dockerfile/web/tomcat/tomcat-app2]#vim index.html 
<h1>tomcat testapp2</h1>

修改docker build脚本centos-tomcat-web:app1改成centos-tomcat-web:app2即可,表示这是app2的镜像

[[email protected] /data/dockerfile/web/tomcat/tomcat-app2]#vim build-command.sh 
#!/bin/bash
# Dockerfile build shell
docker build -t 172.20.7.50/baseimages/centos-tomcat-web:app2 .

查看所有文件,上述除了build-command.sh、index.html需要修改其它不用动保持原样

[[email protected] /data/dockerfile/web/tomcat/tomcat-app2]#ll
total 24
-rw-r--r-- 1 root root   77 Nov 23 20:56 build-command.sh
-rw-r--r-- 1 root root  398 Nov 23 20:15 Dockerfile
-rw-r--r-- 1 root root   25 Nov 23 20:15 index.html
-rwxr-xr-x 1 root root  205 Nov 23 20:15 run_tomcat.sh
-rw------- 1 root root 7524 Nov 23 20:15 server.xml

开始制作第二个镜像文件

[[email protected] /data/dockerfile/web/tomcat/tomcat-app2]#bash build-command.sh 
Sending build context to Docker daemon  13.31kB
Step 1/9 : FROM 172.20.7.50/baseimages/centos-tomcat:8.5.33
 ---> 872c70dd2181
Step 2/9 : LABEL maintainer "mr.luo <[email protected]>"
 ---> Using cache
 ---> e97895b822f7
Step 3/9 : RUN mkdir -p /data/tomcat/webapps/myapp && useradd tomcat
 ---> Using cache
 ---> 72bb5e03ae16
Step 4/9 : ADD index.html  /data/tomcat/webapps/myapp
 ---> 5fb4cdb0e0ca
Step 5/9 : ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
 ---> ca7037e43dd6
Step 6/9 : COPY server.xml /apps/tomcat/conf/server.xml
 ---> 944ac27defb1
Step 7/9 : RUN chown -R tomcat.tomcat /apps /data/tomcat/webapps
 ---> Running in ec169b000f74
Removing intermediate container ec169b000f74
 ---> 168b359f2e83
Step 8/9 : EXPOSE 8080
 ---> Running in 86726c38a44a
Removing intermediate container 86726c38a44a
 ---> d4f8cf67c70c
Step 9/9 : CMD ["/apps/tomcat/bin/run_tomcat.sh"]
 ---> Running in b323b8b073ca
Removing intermediate container b323b8b073ca
 ---> 612035ab8d3e
Successfully built 612035ab8d3e
Successfully tagged 172.20.7.50/baseimages/centos-tomcat-web:app2

查看所有镜像文件

[[email protected] /data/dockerfile/web/tomcat/tomcat-app2]#docker images 
REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
172.20.7.50/baseimages/centos-tomcat-web   app2                612035ab8d3e        10 seconds ago      787MB
172.20.7.50/baseimages/centos-tomcat-web   app1                3714c600c51e        37 minutes ago      787MB
172.20.7.50/baseimages/centos-tomcat       8.5.35              872c70dd2181        2 hours ago         773MB
172.20.7.50/baseimages/centos7.5-jdk       8.191               19e9d5aa96b6        2 hours ago         759MB
172.20.7.50/baseimages/centos-base         7.5.1804            1f6c2c9e089e        2 hours ago         363MB
centos                                     7.5.1804            76d6bc25b8a5        6 weeks ago         200MB

启动容器、并查看当前运行的容器列表

[[email protected] /data/dockerfile/web/tomcat/tomcat-app2]#docker run --name tom2 -d -p 8802:8080 172.20.7.50/baseimages/centos-toat-web:app2
e9f08fce2923fc9dd49edf12472d2ef98994126e920168da80c8694ef4a8f0bc
[[email protected] /data/dockerfile/web/tomcat/tomcat-app2]#docker ps
CONTAINER ID        IMAGE                                           COMMAND                  CREATED             STATUS              PORTS                    NAMES
e9f08fce2923        172.20.7.50/baseimages/centos-tomcat-web:app2   "/apps/tomcat/bin/ru…"   6 seconds ago       Up 6 seconds        0.0.0.0:8802->8080/tcp   tom2
6b77dd85d8a7        172.20.7.50/baseimages/centos-tomcat-web:app1   "/apps/tomcat/bin/ru…"   14 minutes ago      Up 14 minutes       0.0.0.0:8801->8080/tcp   tom1

用物理机的浏览器打开访问tom2是否正常工作

技术分享图片

有什么不足的地方还望各位大表哥在评论区指出来!!!

以上是关于Docker容器-构建Tomcat业务镜像的主要内容,如果未能解决你的问题,请参考以下文章

Docker:在 Tomcat 镜像构建期间访问 MySQL 容器

docker 安装tomcat 设置容器开机自启动 并commit构建新的镜像

Docker之三----java业务镜像制作

构建Nginx+Tomcat+MYSQLDocker容器实战

docker jetty10 启动 war

基于tomcat基础镜像构建各种业务镜像范例如APP