1. 基于centos+jdk基础镜像
FROM centos7-jdk-1.0
2. 拷贝源文件到工作目录
- tomcat
- bin
- setenv.sh
- start_tomcat.sh
- app
- apache-tomcat-8.5.100.tar.gz
- conf
- server.xml
- bin
3. 安装tomcat
3.1 解压安装包
tar -zxvf ${WORK_DIR}/tomcat/app/apache-tomcat-${CATALINA_VERSION}.tar.gz -C ${SRC_SERVER_DIR} && \
3.2 拷贝setenv.sh文件
# 拷贝文件到tomcat安装目录 \
chmod +x ${WORK_DIR}/tomcat/bin/setenv.sh && \
cp ${WORK_DIR}/tomcat/bin/setenv.sh ${SRC_SERVER_DIR}/apache-tomcat-${CATALINA_VERSION}/bin && \
3.3 拷贝tomcat配置文件
cp -r ${WORK_DIR}/tomcat/conf/* ${SRC_APP_DIR}/tomcat/conf/ && \
3.4 拷贝启动脚本
cp ${WORK_DIR}/tomcat/bin/start_tomcat.sh ${SRC_APP_DIR}/tomcat/bin/ && \
chmod +x ${SRC_APP_DIR}/tomcat/bin/start_tomcat.sh && \
3.5 设置entrypoint命令
## 创建数据、日志目录
## 创建目录
ENV MKDIR_TOMCAT="mkdir -p $SRC_LOG_DIR/tomcat"
## 将SRC拷贝到TARGET目录
ENV COPY_EXPORT="cp -r -t $TGT_DIR $SRC_DIR/*"
## CHMOD
ENV CHOWN_ADMIN="chown -R $SYS_USER_GROUP:$SYS_USER $TGT_DIR $SRC_DIR"
## 启动脚本
ENV START_TOMCAT="su $SYS_USER -c \"$TGT_APP_DIR/tomcat/bin/start_tomcat.sh\""
## 进入容器时默认执行的命令
ENTRYPOINT /usr/sbin/sshd && \
/usr/sbin/crond && \
echo $MKDIR_TOMCAT && $MKDIR_TOMCAT && \
echo $COPY_EXPORT && $COPY_EXPORT && \
echo $CHOWN_ADMIN && $CHOWN_ADMIN && \
echo $START_TOMCAT && $START_TOMCAT && \
sleep 9999999d
4. 配置文件示例
4.1 server.xml
一个tomcat内启动多个应用app1和app2
<Service name="app1">
<Connector port="8001" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="9001" URIEncoding="UTF-8"
maxParameterCount="1000" maxSpareThreads="750" maxThreads="1000" minSpareTHreads="" acceptCount="1000"
useBodyEncodingForURI="true"/>
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/export/App/app1" path="/" reloadable="false"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/export/Logs/app1"
prefix="access_log" suffix=".log"
pattern="%h %l %u %t "%r" %s %b"/>
</Host>
</Engine>
</Service>
<Service name="app2">
<Connector port="8002" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="9002" URIEncoding="UTF-8"
maxParameterCount="1000" maxSpareThreads="750" maxThreads="1000" minSpareTHreads="" acceptCount="1000"
useBodyEncodingForURI="true"/>
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/export/App/app2" path="/" reloadable="false"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="/export/Logs/app2"
prefix="access_log" suffix=".log"
pattern="%h %l %u %t "%r" %s %b"/>
</Host>
</Engine>
</Service>
4.2 setenv.sh
设置环境变量和Java启动参数
# TOMCAT
export CATALINA_HOME="/export/servers/apache-tomcat-8.5.100"
export CATALINA_BASE="/export/servers/apache-tomcat-8.5.100"
export CATALINA_OUT="/export/Logs/tomcat/catalina.out"
export CATALINA_PID="$CATALINA_BASE/logs/tomcat.pid"
# JAVA
export JAVA_HOME="/export/servers/jdk1.8.0_391"
export JAVA_OPTS="-server -Xms5120m -Xmx5120m"
4.3 start_tomcat.sh
tomcat启动脚本,手动指定配置文件位置
#!/bin/bash
CATALINA_HOME="/export/servers/apache-tomcat-8.5.100"
$CATALINA_HOME/bin/startup.sh -config /export/App/tomcat/conf/server.xml &