一、环境介绍
操作系统:CentOS 7.9
DockerCE:20.10.9
其他软件:
- apache-tomcat-8.5.72.tar.gz(基于官方版本,对安全和性能做了一系列的优化,并将一个测试war包提前放到webapps目录下)
- jdk1.8_u301-linux-x64.tar.gz
- dockerfile
dockerfile完整内容
#dockerfile
FROM rockylinux/rockylinux:latest
MAINTAINER cnskyleeADD jdk1.8_u301-linux-x64.tar.gz /usr
ADD apache-tomcat-8.5.72.tar.gz /ENV JAVA_HOME /usr/jdk1.8.0_301
ENV CLASSPATH .:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
ENV PATH $JAVA_HOME/bin:$PATH:$HOME/bin
ENV CATALINA_HOME /apache-tomcat-8.5.72
ENV CATALINA_BASE /apache-tomcat-8.5.72EXPOSE 18170
EXPOSE 30011CMD /apache-tomcat-8.5.72/bin/catalina.sh run
备注:这里有一个坑,需要给大家标出来。就是基础镜像不要选择alpine:latest,使用这个基础镜像,构建镜像没问题,但是在运行容器的时候会失败,始终提示/usr/jdk1.8.0_301/bin/java找不到文件。查询了一上午,没解决,下午换成Rocky Linux的docker镜像,一切正常了。
二、构建镜像
# ls -ltr
total 151380
-rwxr-xr-x 1 root root 9485325 Oct 17 22:21 apache-tomcat-8.5.72.tar.gz
-rw-r--r-- 1 root root 145521228 Oct 17 22:32 jdk1.8_u301-linux-x64.tar.gz
-rwxr-xr-x 1 root root 457 Oct 18 02:40 dockerfile
# docker build -t tomcat-rocky:v8.5.72 .
Sending build context to Docker daemon 879.1MB
Step 1/12 : FROM rockylinux/rockylinux:latest
---> 333da17614b6
Step 2/12 : MAINTAINER cnskylee from (cnskylee@)
---> Running in 5a4ff3053ec9
Removing intermediate container 5a4ff3053ec9
---> 475be38fe1c6
Step 3/12 : ADD jdk1.8_u301-linux-x64.tar.gz /usr
---> 77e3d28164e5
Step 4/12 : ADD apache-tomcat-8.5.72.tar.gz /
---> 0528a91dafd9
Step 5/12 : ENV JAVA_HOME /usr/jdk1.8.0_301
---> Running in b7d8399af85d
Removing intermediate container b7d8399af85d
---> 4236e293c567
Step 6/12 : ENV CLASSPATH .:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
---> Running in 20e049a5b7b5
Removing intermediate container 20e049a5b7b5
---> 30cb418a4131
Step 7/12 : ENV PATH $JAVA_HOME/bin:$PATH:$HOME/bin
---> Running in 944f6ec16879
Removing intermediate container 944f6ec16879
---> 5fb0e8b44787
Step 8/12 : ENV CATALINA_HOME /apache-tomcat-8.5.72
---> Running in 11b1ecfbc676
Removing intermediate container 11b1ecfbc676
---> 842897ad3e7c
Step 9/12 : ENV CATALINA_BASE /apache-tomcat-8.5.72
---> Running in 7e6d6d1b4aa8
Removing intermediate container 7e6d6d1b4aa8
---> d01e1ffaddaa
Step 10/12 : EXPOSE 18170
---> Running in 482fcb5d6dea
Removing intermediate container 482fcb5d6dea
---> d2a95a179a7f
Step 11/12 : EXPOSE 30011
---> Running in fcd6238626ca
Removing intermediate container fcd6238626ca
---> f699b624b4f7
Step 12/12 : CMD /apache-tomcat-8.5.72/bin/catalina.sh run
---> Running in de1355b68c22
Removing intermediate container de1355b68c22
---> 081fc550edc2
Successfully built 081fc550edc2
Successfully tagged tomcat-rocky:v8.5.72
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-rocky v8.5.72 081fc550edc2 14 minutes ago 599MB
rockylinux/rockylinux latest 333da17614b6 3 months ago 227MB
三、运行容器
# docker run -dti -p 30011:30011 -p 18170:18170 tomcat-rocky:v8.5.72
9a531c847c12cf0fa1712a8a61e83a598be5ac2c00e5343c22708f7c5b13a8d4
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9a531c847c12 tomcat-rocky:v8.5.72 "/bin/sh -c '/apache…" 6 seconds ago Up 4 seconds 0.0.0.0:18170->18170/tcp, :::18170->18170/tcp, 0.0.0.0:30011->30011/tcp, :::30011->30011/tcp eager_ritchie
四、访问测试
1. 集成的用于Prometheus监控的metrics
2. 测试war包
OK,使用dockerfile来构建一个简单的Tomcat镜像,到这里就结束了。