通过Dockerfile编写tomcat镜像

一、使用原始镜像做基础镜像

1.1 基础镜像的Dockerfile

  1. 删除自带yum源,下载自定义的repo文件【引用了阿里云的源】
  2. 装了一些基础的软件包,gcc,telnet,wget
  3. 安装jdk,把java命令软链接到/usr/bin路径下
  4. 通过配置java.security解决Tomcat启动缓慢的原因
FROM centos:7
MAINTAINER lijun registry.moguyun.com
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN rm -f /etc/yum.repos.d/* && curl -o /etc/yum.repos.d/CentOS-Base.repo http://172.18.175.38/down/CentOS-Base.repo
RUN cat /etc/yum.repos.d/CentOS-Base.repo && yum update -y
RUN yum install -y make gcc gcc-c++ net-tools wget telnet
RUN curl -o /opt/jdk-8u211-linux-x64.tar.gz http://172.18.175.38/down/jdk-8u211-linux-x64.tar.gz
WORKDIR /opt
RUN tar zxf jdk-8u211-linux-x64.tar.gz && rm -f jdk-8u211-linux-x64.tar.gz
RUN ln -s /opt/jdk1.8.0_211/bin/java /usr/bin/java
RUN sed -i "s|securerandom.source=file:/dev/random|securerandom.source=file:/dev/./urandom|g" /opt/jdk1.8.0_211/jre/lib/security/java.security
COPY ./sysinit.sh /bin/sysinit.sh
RUN cat /bin/sysinit.sh
RUN chmod +x /bin/sysinit.sh
CMD ["/bin/sysinit.sh"]

1.2 系统初始化脚本

  1. Shell 创建文件的文件大小指定为不限制
  2. 指定每个进程所使用的秒数指定为不限制
  3. Shell可使用的虚拟内存指定为不限制
  4. 打开文件最大数为64000
  5. 可以使用的常驻内存为不限制
  6. 可以运行的并发进程数为64000
  7. 配置JAVA_HOME和PATH
#!/bin/sh
sysctl vm.overcommit_memory=1

ulimit -f unlimited
ulimit -t unlimited
ulimit -v unlimited
ulimit -n 64000
ulimit -m unlimited
ulimit -u 64000

export JAVA_HOME=/opt/jdk1.8.0_211
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export LANG=en_US.UTF-8

echo "
export JAVA_HOME=$JAVA_HOME
export JRE_HOME=$JRE_HOME
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export LANG=en_US.UTF-8
" >> /etc/profile

source /etc/profile

二、制作tomcat应用镜像

2.1 tomcat镜像Dockerfile

  1. 删除原来的sysinit.sh
  2. 把apache-tomcat-9.0.22拷贝到镜像里面的/opt/tomcat目录
  3. 把pinpoint-agent-1.7.4拷贝到镜像里面的/opt目录
  4. 拷贝sysinit.sh拷贝到镜像里面的/bin目录
  5. 把arthas拷贝到镜像里面的/opt目录
FROM registry.moguyun.com/centos7-jdk8:latest

ENV CATALINA_HOME /opt/tomcat
ENV TOMCAT_VERSION apache-tomcat-9.0.22
ENV PINPOINT_VERSION pinpoint-agent-1.7.4
ENV RUN_OPTION "-server -Xms256m -Xmx1g -Duser.timezone=GMT+08"
ENV PNAME "AppName"
ENV RUN_PORT 8080

RUN rm -f /bin/sysinit.sh

ADD $TOMCAT_VERSION $CATALINA_HOME
ADD $PINPOINT_VERSION /opt/$PINPOINT_VERSION
COPY sysinit.v4.sh  /bin/sysinit.sh
COPY arthas-boot.jar /opt/arthas-boot.jar

WORKDIR $CATALINA_HOME

EXPOSE 8080
ENTRYPOINT ["sh","-c","/bin/sysinit.sh \"$RUN_OPTION\" \"$PNAME\" \"$RUN_PORT\""]

2.2 tomcat镜像的sysinit.sh

  1. 通过CATALINA_OPTS把pinpoint配置到tomcat里面抓取信息
  2. 配置启动端口号,默认为8080
  3. 通过tomcat用户启动catalina.sh,启动时会自动加载CATALINA_OPTS

] # vim sysinit.v4.sh

#!/bin/sh

CATALINA_OPTS=/opt/tomcat
AGENT_JAR=pinpoint-bootstrap-1.7.4.jar
PINPOINT_VERSION=pinpoint-agent-1.7.4
AGENT_PATH=/opt/pinpoint-agent-1.7.4
HOST_FILE=/opt/qwhosts

echo "RUN_OPTION=\"${1}\" PNAME=\"${2}\" RUN_PORT=\"${3}\" DUBBO_PORT=\"${4}\""

hname=`hostname`;
agentId=${hname:0-10};
CATALINA_OPTS="-server -Duser.timezone=GMT+08 ${1} "
if [ "" != "${2}" ]; then
  CATALINA_OPTS="$CATALINA_OPTS -javaagent:'$AGENT_PATH'/'$AGENT_JAR' -Dpinpoint.agentId=app-${agentId} -Dpinpoint.applicationName=${2}"
fi

echo CATALINA_OPTS="$CATALINA_OPTS"

if [ "${3}" != "" ] && [ "${3}" != "0" ]; then
  sed -i "s|8080|${3}|g" /opt/tomcat/conf/server.xml
  sed -i "s|8005|1${3}|g" /opt/tomcat/conf/server.xml
fi

if [ -f "$HOST_FILE" ]; then
  cat $HOST_FILE >> /etc/hosts
fi

cat /bin/sysinit.sh
chown -R tomcat:tomcat /opt

su tomcat -s /bin/bash -c "export CATALINA_OPTS=\"$CATALINA_OPTS\" && cd $CATALINA_HOME/bin && ./catalina.sh run 2>&1 | tee ../logs/catalina.out"
  • 附:企业微信tomcat基础镜像sysinit.v5.sh,去掉了pinpoint

] # vim sysinit.v5.sh

#!/bin/sh

CATALINA_OPTS=/opt/tomcat
HOST_FILE=/opt/qwhosts

echo "RUN_OPTION=\"${1}\" RUN_PORT=\"${2}\""

CATALINA_OPTS="-server -Duser.timezone=GMT+08 ${1} "

echo CATALINA_OPTS="$CATALINA_OPTS"

if [ "${2}" != "" ] && [ "${2}" != "0" ]; then
  sed -i "s|8080|${2}|g" /opt/tomcat/conf/server.xml
  sed -i "s|8005|1${2}|g" /opt/tomcat/conf/server.xml
fi

if [ -f "$HOST_FILE" ]; then
  cat $HOST_FILE >> /etc/hosts
fi

cat /bin/sysinit.sh
chown -R tomcat:tomcat /opt

su tomcat -s /bin/bash -c "export CATALINA_OPTS=\"$CATALINA_OPTS\" && cd $CATALINA_HOME/bin && ./catalina.sh run 2>&1 | tee ../logs/catalina.out"

 

三、制作业务镜像

3.1 通过tomcat启动业务镜像

  1. 通过配置JVM_OPTION来实现tomcat远程调试和集成pinpoint,JVM_OPTION最终会被tomcat启动的时候加载
  2. 停止指定projectName的容器,删除容器
  3. 通过-v映射本地目录到镜像里面,-e加载环境参数启动包含jdk的tomcat镜像
#!/bin/sh
project=$1
pro=${project//_/-}
projectName="$1-TB"
ipaddr=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}'|head -1);  #获取内网ip地址
ipaddr=${ipaddr:10}   #获取内网地址最后3位
ipaddr2=$(ip addr | awk '/^[0-9]+: / {}; /inet.*global/ {print gensub(/(.*)\/(.*)/, "\\1", "g", $2)}'|head -2|sed -n '2p');  #获取公网地址
pname=${2}
RUN_PORT=${3}
DEBUG_PORT=${4}

JVM_OPTION="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1${DEBUG_PORT} -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=${ipaddr2} -
agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=${DEBUG_PORT}  -

Duser.timezone=GMT+08  -server -Xms256m  -Xmx1024m -Dpinpoint.agentId=${pname}-${ipaddr}  -Dpinpoint.applicationName=${pname} "

echo "JVM_OPTION=$JVM_OPTION"

docker stop $projectName && docker rm $projectName
docker run -d --name $projectName --net=host  \
  -v /opt/image_data:/image_data:rw \
  -v /opt/image_data:/opt/image_data:rw \
  -v /docker/tomcat/logs/$project:/opt/tomcat/logs \
  -v /programs/$project/target/$pro:/opt/tomcat/webapps/$pro \
  -e RUN_OPTION="$JVM_OPTION" -e RUN_PORT=$RUN_PORT \
  registry.moguyun.com/tomcat-jdk8

sleep 2s

tail -f ../logs/$project/catalina.out

3.2 启动小店镜像脚本

  1. 编写financialshop.sh启动小店脚本
  2. 传入项目名称,进程名称,运行端口,远程调试tomcat debug端口
/docker/tomcat/bin/tomcat-debug.run financial-shop-server financial 6223 8983

 

你可能感兴趣的:(#,深入浅出Docker)