1、创建Dockerfile文件
# Pull base image FROM ubuntu:14.04 MAINTAINER shencq "[email protected]" # update source RUN echo "deb http://archive.ubuntu.com/ubuntu trusty main universe"> /etc/apt/sources.list RUN apt-get update # Install curl RUN apt-get -y install curl # Install JDK 7 RUN cd /tmp && curl -L ‘http://download.oracle.com/otn-pub/java/jdk/7u65-b17/jdk-7u65-linux-x64.tar.gz‘ -H ‘Cookie: oraclelicense=accept-securebackup-cookie; gpw_e24=Dockerfile‘ | tar -xz RUN mkdir -p /usr/lib/jvm RUN mv /tmp/jdk1.7.0_65/ /usr/lib/jvm/java-7-oracle/ # Set Oracle JDK 7 as default Java RUN update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-7-oracle/bin/java 300 RUN update-alternatives --install /usr/bin/javac javac /usr/lib/jvm/java-7-oracle/bin/javac 300 ENV JAVA_HOME /usr/lib/jvm/java-7-oracle/ # Install tomcat7 RUN cd /tmp && curl -L ‘http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.8/bin/apache-tomcat-7.0.8.tar.gz‘ | tar -xz RUN mv /tmp/apache-tomcat-7.0.8/ /opt/tomcat7/ ENV CATALINA_HOME /opt/tomcat7 ENV PATH $PATH:$CATALINA_HOME/bin ADD tomcat7.sh /etc/init.d/tomcat7 RUN chmod 755 /etc/init.d/tomcat7 # Expose ports. EXPOSE 8080 # Define default command. ENTRYPOINT service tomcat7 start && tail -f /opt/tomcat7/logs/catalina.out
2、创建tomcat7.sh文件
export JAVA_HOME=/usr/lib/jvm/java-7-oracle/ export JAVA_OPTS="-Dfile.encoding=UTF-8 \ -Dcatalina.logbase=/var/log/tomcatProd -Dnet.sf.ehcache.skipUpdateCheck=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseParNewGC -XX:MaxPermSize=128m -Xms512m -Xmx512m" export PATH=$JAVA_HOME/bin:$PATH TOMCAT_HOME=/opt/tomcat7 SHUTDOWN_WAIT=20 export CATALINA_OPTS="-Xmx512m" export CATALINA_BASE=/opt/tomcat7 tomcat_pid() { echo `ps aux | grep org.apache.catalina.startup.Bootstrap | grep $CATALINA_BASE | grep -v grep | awk ‘{ print $2 }‘` } start() { pid=$(tomcat_pid) if [ -n "$pid" ] then echo "Tomcat is already running (pid: $pid)" else # Start tomcat echo "Starting tomcat" #ulimit -n 100000 umask 007 $TOMCAT_HOME/bin/startup.sh fi return 0 } stop() { pid=$(tomcat_pid) if [ -n "$pid" ] then echo "Stoping Tomcat" if [ "$USER" == "$TOMCAT_USER" ] then $TOMCAT_HOME/bin/shutdown.sh else /bin/su -p -s /bin/sh $TOMCAT_USER $TOMCAT_HOME/bin/shutdown.sh fi let kwait=$SHUTDOWN_WAIT count=0; until [ `ps -p $pid | grep -c $pid` = ‘0‘ ] || [ $count -gt $kwait ] do echo -n -e "\nwaiting for processes to exit"; sleep 1 let count=$count+1; done if [ $count -gt $kwait ]; then echo -n -e "\nkilling processes which didn‘t stop after $SHUTDOWN_WAIT seconds" kill -9 $pid fi else echo "Tomcat is not running" fi return 0 } case $1 in start) start ;; stop) stop ;; restart) stop start ;; status) pid=$(tomcat_pid) if [ -n "$pid" ] then echo "Tomcat is running with pid: $pid" else echo "Tomcat is not running" fi ;; esac exit 0
3、构建镜像
docker build -t shencq/tomcat7 .
4、运行镜像
docker run -d -p 8090:8080 shencq/tomcat7
5、访问tomcat
默认情况下,tomcat会占用8080端口,刚才在启动container的时候,指定了 -p 8090:8080,映射到宿主机端口就是8090。
http://<host>:8090 #host为主机IP
时间: 2024-10-24 22:49:02