使用Docker创建Tomcat镜像

随笔 2016-10-06

(请确认docker已经安装) 1.新建一个tomcat7.0的文件夹,创建docker相关文件,并下载解压tomcat7.0和jdk的安装包。

1.png

此时:

2.png

2.编写Dockerfile文件

 FROM sshd:dockerfile 

 #设置承自我们创建的sshd镜像 
 MAINTAINER waitfish from dockerpool.com  
 #下面是一些创建者的基本信息 

 #设置环境变量,所有操作是非交互式的 
 ENV DEBIAN_FRONTEND noninteractive 

 RUN echo "Asia/Shanghai " > /etc/timezone && \ 
           dpkg-reconfigure  -f  noninteractive tzdata 
 #注意这里更改系统的时区设置,因为在web应用中经常会用到时区这个系统变量

 #安装跟tomcat用户认证相关的软件 

 RUN apt-get install  -yq --no-install-recommends wget pwgen ca-certificates &&  \ 
 apt-get clean && \ 
 rm —rf /var/lib/apt/lists/* 

 #设置tomcat的环境量 
 ENV CATALINA_HOME /tomcat 
 ENV JAVA_HOME /jdk 

 #复制tomcat和jdk文件到镜像中 
 ADD apache—tomcat—7.0.72 /tomcat
 ADD jdk /jdk
 ADD create_tomcat_admin_user.sh  /create_tomcat_admin_user.sh
 ADD run.sh /run.sh 
 RUN chmod +x /*.sh 
 RUN chmod +x /tomcat/bin/*.sh
 EXPOSE 8080
 CMD ["/run.sh"] 

3.创建create_tomcat_admin_user.sh和run.sh脚本运行tomcat

create_tomcat_admin_user.sh

 #!/bin/bash 
if [-f /.tomcat_admin_created]; then 
echo "Tomcat, 'admin' user ready created" 
exit 0 
fi

#generate password 
PASS=${TOMCAT_PASS:-$(pwgen -s 12 1 )}, 
_word=$([ ${TOMCAT_PASS} ] && echo "preset" || echo "random") 
echo"=> Creating and admin user with a ${_word} password in Tomcat "  
sed -i -r 's/</tomcat—user>//' ${CATALINA_HOMEl/conf/tomcat—users.xml    
echo '<role rolename="manager—gui"/>' >>${CATALINA_HOME/conf/tomcat—users.xml     
echo '<role rolename="manager—script"/>' >>${CATALINA_HOME/conf/tomcat—users.xm1      
echo '<role rolename—="manager-jmx"/>' >>${CATALINA_HOME/conf/tomcat—users.xml      
echo '<role rolename="admin-gui"/>' >>${CATALINA_HOME/conf/tomcat—users.xml      
echo '<role rolename="admin-script"/>' >>${CATALINA_HOME/conf/tomcat—users.xml    
echo"<user username=\"admin\" password="${PASS}\" roles=\"manager-gui,manager—script, manager—jmx, admin—gui, admin—script\"/>" >>${CATALINA_HOME/conf/tomcat—users.xml 
echo '</tomcat—userO>' >>${CATALINA_HOME/conf/tomcat—users.xml 
echo "=>Done!" 
touch /.tomcat_admin_created 
echo "==============================================================="
echo "You can now configure to this Tomcatserver using:"
echo ""
eced -i -r 's/</tomcat—user>//' ${CATALINA_HOMEl/conf/tomcat—users.xml
echo '<role rolename="manager—gui"/>' >>${CATALINA_HOME/conf/tomcat—users.xml
echo '<role rolename="manager—script"/>' >>${CATALINA_HOME/conf/tomcat—users.xm1
echo '<role rolename—="manager-jmx"/>' >>${CATALINA_HOME/conf/tomcat—users.xml
echo '<role rolename="admin-gui"/>' >>${CATALINA_HOME/conf/tomcat—users.xml 
echo '<role rolename="admin-script"/>' >>${CATALINA_HOME/conf/tomcat—users.xml
echo"<user username=\"admin\" password="${PASS}\" roles=\"manager-gui,manager—script, manager—jmx, admin—gui, admin—script\"/>" >>${CATALINA_HOME/conf/tomcat—users.xml
echo '</tomcat—userO>' >>${CATALINA_HOME/conf/tomcat—users.xml
echo "=>Done!"
touch /.tomcat_admin_created
echo "==============================================================="
echo "You can now configure to this Tomcatserver using:"
echo ""
echo "admin:$(PASS)"
echo ""
echo "==============================================================="
echo "admin:$(PASS)" 
echo ""
echo "==============================================================="

run.sh如下

  #!/bin/bash 
  if [ ! -f ./create tomcat admin user.sh];then
     ./create tomcat admin user.sh
  fi
  /usr/sbin/sshd —D & 
  exec ${CATALINA_HOME}/bin/catalina.sh run 

4.编译和创建镜像

docker build -t tomcat7.0:jdk1.8 .    

之后使用 docker images 可以看到最新的镜像,直接运行镜像

 docker run -d -P  tomcat7.0:jdk1.8

docker logs id 可以查看日志信息和获取密码

使用docker ps 查看端口使用,并登陆tomcat

总结:这样可以方便地统一团队的开发环境,并大大节约了配置所花的时间,其它镜像创建方法也是类似的。


本文由 Tony 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

赏个馒头吧