• 创建 tomcat 服务的镜像


    如何设计 Tomcat 的 Dockerfile

    $ sudo docker search tomcat |wc -l
    285
    

    在 dockerhub 上搜索与 tomcat 相关的镜像,有如此之多的返回。笔者建议,如是个人开发为图方便,可以随意选择一个镜像,按照提示启动应用即可。若准备在生产环境中使用,或者是几个人、团队协作的项目,上述这些镜像都不是那么合适了。

    原因有三:

    • 每个项目要求的 tomcat 版本不一致
    • 每个项目要求的 tomcat 变量不一致
    • 每个项目要求的 jdk 不一致

    最好的方式应该是由架构师通过 dockerfile 或者其他方式构建好统一的镜像,然后分发给项目组所有成员

    面就以 sun_jdk 1.6|tomcat 7.0|ubuntu 14.04 为环境介绍下如何定制自己的 tomcat 镜像。

    准备工作

    #创建 tomcat7.0_jdk1.6 文件夹
    $ mkdir tomcat7.0_jdk1.6
    $ cd tomcat7.0_jdk1.6/
    #上传 jdk 到 tomcat7.0_jdk1.6 文件夹下面,也可以到本书的共享网盘下载(下载地址xxx),下载后用 unzip 解压,若没有 unzip 可以先在 windows 机器上用解压软件解压再上传到文件夹底下,也可以使用 apt-get install unzip 来安装解压软件 unzip。
    $ touch Dockerfile run.sh
    #至于 tomcat ,读者可以使用自己熟悉的版本,或者到官方网站下载最新的版本。
    $ wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.56/bin/apache-tomcat-7.0.56.zip
    --2014-10-27 22:25:23--  http://mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.56/bin/apache-tomcat-7.0.56.zip
    Resolving mirror.bit.edu.cn (mirror.bit.edu.cn)... 219.143.204.117, 2001:da8:204:2001:250:56ff:fea1:22
    Connecting to mirror.bit.edu.cn (mirror.bit.edu.cn)|219.143.204.117|:80... connected.
    HTTP request sent, awaiting response... 200 OK
    Length: 9466255 (9.0M) [application/zip]
    Saving to: 'apache-tomcat-7.0.56.zip'
    
    100%[===========================================================================================================>] 9,466,255    152KB/s   in 70s
    
    2014-10-27 22:26:34 (131 KB/s) - 'apache-tomcat-7.0.56.zip' saved [9466255/9466255]
    #同样解压后,目前 tomcat7.0_jdk1.6 目录底下应该是这样的,多余的 zip 已经被我删除。
    $ ls
    Dockerfile  apache-tomcat-7.0.56   jdk  run.sh
    

    Dockerfile 文件和其他脚本文件

    FROM sshd:dockerfile
    #设置继承自我们创建的 sshd 镜像
    MAINTAINER waitfish from dockerpool.com(dwj_zz@163.com)
    #下面是一些创建者的基本信息
    
    #设置环境变量,所有操作都是非交互式的
    ENV DEBIAN_FRONTEND noninteractive
    
    RUN echo "Asia/Shanghai" > /etc/timezone && 
            dpkg-reconfigure -f noninteractive tzdata
    
    #注意这里要更改系统的时区设置,因为在 web 应用中经常会用到时区这个系统变量,默认的 ubuntu 会让你的应用程序发生不可思议的效果哦
    
    #安装跟 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.56 /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"]
    

    创建 tomcat 用户和密码的脚本文件,create_tomcat_admin_user.sh 文件

    #!/bin/bash
    
    if [ -f /.tomcat_admin_created ]; then
        echo "Tomcat 'admin' user already 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-users>//' ${CATALINA_HOME}/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.xml
    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-users>' >> ${CATALINA_HOME}/conf/tomcat-users.xml
    echo "=> Done!"
    touch /.tomcat_admin_created
    
    echo "========================================================================"
    echo "You can now configure to this Tomcat server using:"
    echo ""
    echo "    admin:${PASS}"
    echo ""
    echo "========================================================================"
    

    run.sh 脚本文件

    #!/bin/bash
    
    if [ ! -f /.tomcat_admin_created ]; then
        /create_tomcat_admin_user.sh
    fi
    /usr/sbin/sshd -D &
    exec ${CATALINA_HOME}/bin/catalina.sh run
    

    创建镜像、测试镜像

    #删去了一下无关紧要的输出
    $ sudo docker build -t tomcat7.0:jdk1.6 .
    Sending build context to Docker daemon 234.8 MB
    Sending build context to Docker daemon
    Step 0 : FROM sshd:dockerfile
     ---> 570c26a9de68
    Step 1 : MAINTAINER waitfish from dockerpool.com(dwj_zz@163.com)
     ---> Using cache
     ---> 5c6b90057a1d
    Step 2 : ENV DEBIAN_FRONTEND noninteractive
     ---> Using cache
     ---> e06feb0790d7
    Step 3 : RUN echo "Asia/Shanghai" > /etc/timezone &&         dpkg-reconfigure -f noninteractive tzdata
     ---> Running in 6dba2d312627
    
    Current default time zone: 'Asia/Shanghai'
    Local time is now:      Tue Oct 28 13:47:08 CST 2014.
    Universal Time is now:  Tue Oct 28 05:47:08 UTC 2014.
    
     ---> a1dccb384edb
    Removing intermediate container 6dba2d312627
    ...
    Setting up pwgen (2.06-1ubuntu4) ...
     ---> e0e4ab118cda
    Removing intermediate container aee38d8ab936
    Step 5 : ENV CATALINA_HOME /tomcat
     ---> Running in 8d0d7176fb7e
     ---> e4d8891f4e86
    Removing intermediate container 8d0d7176fb7e
    Step 6 : ENV JAVA_HOME /jdk
     ---> Running in 53ce1fa9b8a0
     ---> f17a13a87981
    Removing intermediate container 53ce1fa9b8a0
    Step 7 : ADD apache-tomcat-7.0.56 /tomcat
     ---> ca1fa71b4130
    Removing intermediate container 27e2d96bcb78
    Step 8 : ADD jdk /jdk
     ---> d7a595c4c4f9
    Removing intermediate container 00d980ad2cad
    Step 9 : ADD create_tomcat_admin_user.sh /create_tomcat_admin_user.sh
     ---> 5055ca84decc
    Removing intermediate container 220922d934ce
    Step 10 : ADD run.sh /run.sh
     ---> da469edb1022
    Removing intermediate container f0dde8563174
    Step 11 : RUN chmod +x /*.sh
     ---> Running in 71564c350a2e
     ---> 5f566293e37c
    Removing intermediate container 71564c350a2e
    Step 12 : EXPOSE 8080
     ---> Running in 055c41de3bd8
     ---> b1213c1bc920
    Removing intermediate container 055c41de3bd8
    Step 13 : CMD /run.sh
     ---> Running in 5dbe1220a559
     ---> ce78537c247d
    Removing intermediate container 5dbe1220a559
    Successfully built ce78537c247d
    #查看下目前拥有的镜像
    $ sudo docker images
    REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
    tomcat7.0           jdk1.6              ce78537c247d        9 minutes ago       473.3 MB
    nginx               albb                8e333a6f1d10        14 hours ago        567.6 MB
    nginx               stable              4e3936e36e31        15 hours ago        262.3 MB
    apache              ubuntu              06d84c79e905        16 hours ago        263.8 MB
    sshd                dockerfile          570c26a9de68        26 hours ago        246.5 MB
    sshd                ubuntu              7aef2cd95fd0        39 hours ago        255.2 MB
    debian              latest              61f7f4f722fb        7 days ago          85.1 MB
    busybox             latest              e72ac664f4f0        3 weeks ago         2.433 MB
    ubuntu              14.04               ba5877dc9bec        3 months ago        192.7 MB
    ubuntu              latest              ba5877dc9bec        3 months ago        192.7 MB
    
    #运行一个测试的 tomcat 容器
    $ sudo docker run -d -P tomcat7.0:jdk1.6
    3cd4238cb32a713a3a1c29d93fbfc80cba150653b5eb8bd7629bee957e7378ed
    #通过 docker logs 得到 tomcat 的密码
    $ sudo docker logs 3cd
    => Creating and admin user with a random password in Tomcat
    => Done!
    ========================================================================
    You can now configure to this Tomcat server using:
    
        admin:aBwN0CNCPckw
    
    ========================================================================
    Oct 28, 2014 2:02:24 PM org.apache.catalina.core.AprLifecycleListener init
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /jdk/jre/lib/amd64/server:/jdk/jre/lib/amd64:/jdk/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
    Oct 28, 2014 2:02:24 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["http-bio-8080"]
    Oct 28, 2014 2:02:24 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
    Oct 28, 2014 2:02:24 PM org.apache.catalina.startup.Catalina load
    #查看映射的端口
    $ sudo docker ps
    CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                            NAMES
    3cd4238cb32a        tomcat7.0:jdk1.6    "/run.sh"           4 seconds ago       Up 3 seconds        0.0.0.0:49157->22/tcp, 0.0.0.0:49158->8080/tcp   cranky_wright
    

    在另外一台机器上使用浏览器登陆 输入我们从 docker logs 输出中得到的密码 成功进入管理界面 

    注意事项

    在实际环境中,我们可以通过使用 -v 参数来覆盖 tomcat 的日志文件、程序所在目录、以及与 tomcat 相关的一切配置。

  • 相关阅读:
    http://www.jdon.com/jivejdon/thread/37340
    我的英语死在类似的问题上
    Linux之read命令使用
    SIP注册呼叫流程简介
    sh里的变量 $0 $1 $$ $#
    LTE 逻辑分层和接口协议
    LTE语音业务VOLTE
    shell编程——if语句 if z n f eq ne lt
    高通QXDM抓modem log
    LTE与VOLTE基础知识
  • 原文地址:https://www.cnblogs.com/felixzh/p/5000316.html
Copyright © 2020-2023  润新知