前言
在日常部署java 环境时经常会出现tomcat 版本不同或者java 项目不适用tomcat 作为web容器的情况,为方便使用,制作了一个通用的tomcat Docker 容器,用外部参数灵活判断具体的环境。
环境安装
docker pull centos
docker run --privileged --name centos -h Centos7 -d -i \
-v /Volumes/MacSoft/Downloads:/opt/soft -t centos /usr/sbin/init
docker exec -it centos /bin/bash
Dockerfile
FROM centos
MAINTAINER John.wang "mir355@163.com"
RUN yum install -y --nogpgcheck wget
ADD tomcat.sh /run.sh
RUN chmod 755 /run.sh
# install JDK
RUN wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jdk-8u161-linux-x64.rpm
RUN rpm -ivh jdk-8u161-linux-x64.rpm && rm -rf jdk-8u161-linux-x64.rpm
# install tomcat
RUN wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.30/bin/apache-tomcat-8.5.30.tar.gz
RUN tar xzf apache-tomcat-8.5.30.tar.gz -C /usr/local && rm -rf apache-tomcat-8.5.30.tar.gz
RUN wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.7/bin/apache-tomcat-9.0.7.tar.gz
RUN tar xzf apache-tomcat-9.0.7.tar.gz -C /usr/local && rm -rf apache-tomcat-9.0.7.tar.gz
ENTRYPOINT ["/run.sh"]
run.sh
#!/bin/sh
if [ "$1" == '' ]; then
ver="9"
else
ver="$1"
fi
if [ "$ver" == "9" ]; then
ln -s /usr/local/apache-tomcat-9.0.7 /usr/local/tomcat
fi
if [ "$ver" == "8" ]; then
ln -s /usr/local/apache-tomcat-8.5.30 /usr/local/tomcat
fi
if [ -d "/usr/local/tomcat" ]; then
/usr/local/tomcat/bin/catalina.sh run && tail -f /usr/local/tomcat/logs/catalina.out
else
$ver
fi
保存并使用
docker tag tomcat 127.0.0.1:5000/tomcat
docker push 127.0.0.1:5000/tomcat
# 默认使用tomcat9
docker run -d -h tomcat --name tomcat -v /Volumes/MacSoft/Downloads:/opt/soft -t 127.0.0.1:5000/tomcat
# 增加参数指定为tomcat8
docker run -d -h tomcat --name tomcat -v /Volumes/MacSoft/Downloads:/opt/soft -t 127.0.0.1:5000/tomcat 8
docker exec -it tomcat /bin/bash
转载于:https://blog.51cto.com/13673090/2097323