1. 程式人生 > >Docker tomcat 多版本環境搭建

Docker tomcat 多版本環境搭建

Docker tomcat

前言

在日常部署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 "[email protected]"
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

Docker tomcat 多版本環境搭建