Dockerfile制作tomcat业务镜像

一 工作目录

root@k8s-harbor-01:/data/dockerfile/web/tomcat/tomcat-app1# pwd
/data/dockerfile/web/tomcat/tomcat-app1

二 准备业务配置

2.1 调整JAVA_OPTS

按需求向catalina.sh脚本添加以下内容:

JAVA_OPTS="-server -Xms1g -Xmx1g -Xss512k -Xmn1g -XX:CMSInitiatingOccupancyFraction=65  -XX:+UseFastAccessorMethods -XX:+AggressiveOpts -XX:+UseBiasedLocking -XX:+DisableExplicitGC -XX:MaxTenuringThreshold=10 -XX:NewSize=2048M -XX:MaxNewSize=2048M -XX:NewRatio=2 -XX:PermSize=128m -XX:MaxPermSize=512m -XX:CMSFullGCsBeforeCompaction=5 -XX:+ExplicitGCInvokesConcurrent -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+CMSParallelRemarkEnabled"

2.2 准备server.xml

按需修改server.xml文件内容,比如:

<Host name="localhost"  appBase="/data/tomcat/webapps"  unpackWARs="false" autoDeploy="false">

2.3  准备业务代码

root@k8s-harbor-01:/data/dockerfile/web/tomcat/tomcat-app1# ls
Dockerfile  build-command.sh  catalina.sh  myapp  myapp.tar.gz  server.xml  webapps.tar.gz

三 编写Dockerfile

root@k8s-harbor-01:/data/dockerfile/web/tomcat/tomcat-app1# cat Dockerfile 
FROM 192.168.174.120/baseimages/tomcat-base:8.5.73 

ADD catalina.sh /usr/local/tomcat/bin/catalina.sh
ADD server.xml /usr/local/tomcat/conf/server.xml
ADD webapps.tar.gz /data/tomcat/
ADD myapp.tar.gz /data/tomcat/webapps/

EXPOSE 8080 8443

CMD ["catalina.sh","run"]

四 编写构建镜像脚本

root@k8s-harbor-01:/data/dockerfile/web/tomcat/tomcat-app1# cat build-command.sh 
#!/bin/bash
TAG=$1
docker build -t  192.168.174.120/baseimages/tomcat-app1:${TAG} .
sleep 3
#docker push 192.168.174.120/baseimages/tomcat-app1:${TAG} 

五 构建镜像

root@k8s-harbor-01:/data/dockerfile/web/tomcat/tomcat-app1# ./build-command.sh v1.0
Sending build context to Docker daemon  35.84kB
Step 1/6 : FROM 192.168.174.120/baseimages/tomcat-base:8.5.73
 ---> 97996035932d
Step 2/6 : ADD catalina.sh /usr/local/tomcat/bin/catalina.sh
 ---> 150d60c70080
Step 3/6 : ADD server.xml /usr/local/tomcat/conf/server.xml
 ---> 632dce2ee2c1
Step 4/6 : ADD myapp.tar.gz /data/tomcat/webapps/myapp/
 ---> 2c808d5a7a05
Step 5/6 : EXPOSE 8080 8443
 ---> Running in 575339151d4f
Removing intermediate container 575339151d4f
 ---> 47b28a887efd
Step 6/6 : CMD ["catalina.sh","run"]
 ---> Running in 5822a7d40af5
Removing intermediate container 5822a7d40af5
 ---> 0b2e32fd406f
Successfully built 0b2e32fd406f
Successfully tagged 192.168.174.120/baseimages/tomcat-app1:v1.0

六 验证镜像

6.1 启动镜像

root@k8s-harbor-01:/data/dockerfile/web/tomcat/tomcat-app1# docker run -d -p 8080:8080 --name tomcat-app1  192.168.174.120/baseimages/tomcat-app1:v1.0 
d441dd1bd2c3c0e2f8b0c4d75eb3b5f54d59589829c1bb8fb37e88fc9b8f8109

6.2 查看镜像信息

root@k8s-harbor-01:/data/dockerfile/web/tomcat/tomcat-app1# docker ps
CONTAINER ID   IMAGE                                         COMMAND                  CREATED          STATUS                 PORTS                                                 NAMES
d441dd1bd2c3   192.168.174.120/baseimages/tomcat-app1:v1.0   "catalina.sh run"        28 seconds ago   Up 27 seconds          0.0.0.0:8080->8080/tcp, :::8080->8080/tcp, 8443/tcp   tomcat-app1

6.3 访问web界面

6.4 访问业务路径

root@k8s-harbor-01:/data/dockerfile/web/tomcat/tomcat-app1# curl http://192.168.174.120:8080/myapp/
tomcat docker image test

七 上传镜像到harbor

root@k8s-harbor-01:/data/dockerfile/web/tomcat/tomcat-app1# docker push  192.168.174.120/baseimages/tomcat-app1:v1.0 
posted @ 2021-12-03 14:37  小吉猫  阅读(169)  评论(0编辑  收藏  举报