• JavaEE&Docker 容器示例



    准备:jboss、jdk、一个javaee的war包、Dockerfile

    注:jboss和jdk可以不用提前准备好,在命令中wget也可以,因为我恰好有,就直接复制了

     Dockerfile内容:

    #admln 20150120
    FROM ubuntu:14.04
    MAINTAINER admln <admln@example.com>
    RUN mkdir /usr/jboss
    COPY wildfly-8.2.0.Final /usr/jboss/
    COPY JavaEEDocker.war /usr/jboss/standalone/deployments/
    RUN mkdir /usr/java/
    COPY jdk-7u71-linux-x64.tar.gz /usr/java/
    WORKDIR /usr/java/
    RUN tar -zxf jdk-7u71-linux-x64.tar.gz
    RUN update-alternatives --install /usr/bin/javac javac /usr/java/jdk1.7.0_71/bin
    /javac 100
    RUN update-alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_71/bin/j
    ava 100
    RUN update-alternatives --display java
    RUN java -version
    EXPOSE 8080 9990
    CMD ["/usr/jboss/bin/standalone.sh", "-c", "standalone-full.xml", "-b", "0.0.0.0
    "]

    构建:

    sudo docker build -t="javaee_docker_jboss" .

    构建过程:

    Sending build context to Docker daemon 422.1 MB
    Sending build context to Docker daemon
    Step 0 : FROM ubuntu:14.04
    ---> b39b81afc8ca
    Step 1 : MAINTAINER admln <admln@example.com>
    ---> Using cache
    ---> c6410b257abc
    Step 2 : RUN mkdir /usr/jboss
    ---> Running in faae0476fa09
    ---> bee1d5851e62
    Removing intermediate container faae0476fa09
    Step 3 : COPY wildfly-8.2.0.Final /usr/jboss/
    ---> 8d40f390a11e
    Removing intermediate container a7a4174c0e79
    Step 4 : COPY JavaEEDocker.war /usr/jboss/standalone/deployments/
    ---> 1335bd7ad129
    Removing intermediate container 589fa30b1311
    Step 5 : RUN mkdir /usr/java/
    ---> Running in ac21536064f7
    ---> cb4703bc284c
    Removing intermediate container ac21536064f7
    Step 6 : COPY jdk-7u71-linux-x64.tar.gz /usr/java/
    ---> 35a31c2bc140
    Removing intermediate container f4c26050e511
    Step 7 : WORKDIR /usr/java/
    ---> Running in 1217154d1cca
    ---> a17af238fed9
    Removing intermediate container 1217154d1cca
    Step 8 : RUN tar -zxf jdk-7u71-linux-x64.tar.gz
    ---> Running in dfb046ddb86d
    ---> ea154f6f3896
    Removing intermediate container dfb046ddb86d
    Step 9 : RUN update-alternatives --install /usr/bin/javac javac /usr/java/jdk1.7.0_71/bin/javac 100
    ---> Running in ff061540aae3
    update-alternatives: using /usr/java/jdk1.7.0_71/bin/javac to provide /usr/bin/javac (javac) in auto mode
    ---> c3f4118943cc
    Removing intermediate container ff061540aae3
    Step 10 : RUN update-alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_71/bin/java 100
    ---> Running in 764aa09ab43f
    update-alternatives: using /usr/java/jdk1.7.0_71/bin/java to provide /usr/bin/java (java) in auto mode
    ---> fddc132f2010
    Removing intermediate container 764aa09ab43f
    Step 11 : RUN update-alternatives --display java
    ---> Running in dd123440d778
    java - auto mode
    link currently points to /usr/java/jdk1.7.0_71/bin/java
    /usr/java/jdk1.7.0_71/bin/java - priority 100
    Current 'best' version is '/usr/java/jdk1.7.0_71/bin/java'.
    ---> 0951d9f62f99
    Removing intermediate container dd123440d778
    Step 12 : RUN java -version
    ---> Running in 839afe5e2276
    java version "1.7.0_71"
    Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
    Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)
    ---> f51af884a45f
    Removing intermediate container 839afe5e2276
    Step 13 : EXPOSE 8080 9990
    ---> Running in ed736e99366e
    ---> bce05a7bdb61
    Removing intermediate container ed736e99366e
    Step 14 : CMD /usr/jboss/bin/standalone.sh -c standalone-full.xml -b 0.0.0.0
    ---> Running in fea176d2760b
    ---> 947def6b3995
    Removing intermediate container fea176d2760b
    Successfully built 947def6b3995

    启动容器

     sudo docker run -p 8180:8080 javaee_docker_jboss

    测试:


    tomcat版:

    Dockerfile:

    #admln 20150120
    FROM ubuntu:14.04
    MAINTAINER admln <admln@example.com>
    RUN mkdir /usr/tomcat
    COPY apache-tomcat-7.0.57.tar.gz /usr/tomcat/
    WORKDIR /usr/tomcat/
    RUN tar zxf apache-tomcat-7.0.57.tar.gz
    COPY JavaEEDocker.war /usr/tomcat/apache-tomcat-7.0.57/webapps/
    RUN mkdir /usr/java/
    COPY jdk-7u71-linux-x64.tar.gz /usr/java/
    WORKDIR /usr/java/
    RUN tar -zxf jdk-7u71-linux-x64.tar.gz
    RUN update-alternatives --install /usr/bin/javac javac /usr/java/jdk1.7.0_71/bin
    /javac 100
    RUN update-alternatives --install /usr/bin/java java /usr/java/jdk1.7.0_71/bin/j
    ava 100
    RUN update-alternatives --display java
    RUN java -version
    EXPOSE 8080 9990
    CMD /usr/tomcat/apache-tomcat-7.0.57/bin/startup.sh && tail -f /usr/tomcat/apach
    e-tomcat-7.0.57/logs/catalina.out

    构建启动... ...

    测试:

    tomcat和JBoss有点不一样,JBoss启动后默认一直进行的,而tomcat启动后终端就运行完了

    所以 CMD 命令中我加了一个 tail -f /usr/tomcat/apache-tomcat-7.0.57/logs/catalina.out

    保证终端一直运行。

    不过后来发现 bin/catalina.sh run 命令可以让tomcat启动并且终端不退出

    所以比较推荐的Dockerfile写法是:

    ... ...
    CMD /usr/tomcat/apache-tomcat-7.0.57/bin/catalina.sh run

     


    tomcat 的两三次失败带来的另一个深入理解是 docker 的层级关系

    我修改了Dockerfile 再已同样的名字构建时,由于只修改了第十六步的内容,所以之前的都使用已经构建好的,只是重新构建第十六步那一层(layer),然后把原来的镜像删除生成一个新的

    还有一个就是 docker 容器的即时性,运行完即退出,这一点和 VM 区别也很大,所以限制了 docker 的应用场景


    欲为大树,何与草争;心若不动,风又奈何。
  • 相关阅读:
    Java通过反射加载的类,变量无法注入
    jmeter http并发测试时报错
    spring 自定义注解
    cmd 和powershell 用git 显示乱码
    centos6.8上安装部署 jhipster-registry
    tcpdf最新版 6.2版
    微信Oauth2.0鉴权 40029 问题
    微信支付学习记录1
    win10 localhost 解析为 ipv6地址 ::1 的解决办法
    PHPExcel 导出时乱码
  • 原文地址:https://www.cnblogs.com/admln/p/javaee_docker.html
Copyright © 2020-2023  润新知