docker 运行tomcat 并部署 java web项目
以下tomcat官方镜像中tomcat:7 和tomcat:8的目录。
CATALINA_BASE: /usr/local/tomcat
CATALINA_HOME: /usr/local/tomcat
CATALINA_TMPDIR: /usr/local/tomcat/temp
JRE_HOME: /usr
CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
先将简单的war包上传到服务器放在任意目录,我的war包是docker.war, 这里是放在docker_tomcat/webapps, cd 进去docker_tomcat, 执行如下命令
docker run -it --rm -p 8888:8080 -v $PWD/webapps:/usr/local/tomcat/webapps tomcat:8.0
参数解析:
-it # 是-i 和 -t的简写, 表示以交互式的方式运行容器,加上-d表示后台运行,这里为了截图输出启动日志我用了-it,也可以用-d,再用"docker logs 容器名"命令输出日志
--rm #当容器被停止时自动删除容器
-p 8888:80 #80是为容器中的tomcat设置的端口, 这里表示将80映射到宿主机8888端口, 如果只写-p 80 容器会随机取值32768~61000中较大的端口号来映射到80端口上
-v # 将tomcat中的usr/local/tomcat/webapps目录映射到宿主机当前目录的webapps目录,后面更新jar包直接扔到被映射的宿主机目录中即可
tomcat:8.0 # 镜像名:tag标签
运行之后会输出taomcat的启动日志如下:
# docker run -it --rm -p 8888:8080 -v $PWD/webapps:/usr/local/tomcat/webapps tomcat:8.0
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /docker-java-home/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
22-Oct-2018 05:09:13.274 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.0.53
22-Oct-2018 05:09:13.276 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 29 2018 14:42:45 UTC
22-Oct-2018 05:09:13.277 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.0.53.0
22-Oct-2018 05:09:13.277 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
22-Oct-2018 05:09:13.278 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 4.10.5-1.el6.elrepo.x86_64
22-Oct-2018 05:09:13.278 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
22-Oct-2018 05:09:13.279 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-7-openjdk-amd64/jre
22-Oct-2018 05:09:13.279 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.7.0_181-b01
22-Oct-2018 05:09:13.280 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
22-Oct-2018 05:09:13.280 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
22-Oct-2018 05:09:13.280 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
22-Oct-2018 05:09:13.281 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
22-Oct-2018 05:09:13.282 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
22-Oct-2018 05:09:13.285 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
22-Oct-2018 05:09:13.286 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
22-Oct-2018 05:09:13.286 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
22-Oct-2018 05:09:13.287 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
22-Oct-2018 05:09:13.287 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
22-Oct-2018 05:09:13.288 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
22-Oct-2018 05:09:13.288 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library 1.2.17 using APR version 1.5.1.
22-Oct-2018 05:09:13.288 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
22-Oct-2018 05:09:13.297 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized (OpenSSL 1.1.0f 25 May 2017)
22-Oct-2018 05:09:13.464 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-apr-8080"]
22-Oct-2018 05:09:13.495 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-apr-8009"]
22-Oct-2018 05:09:13.501 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1214 ms
22-Oct-2018 05:09:13.591 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
22-Oct-2018 05:09:13.595 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.53
22-Oct-2018 05:09:13.666 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive /usr/local/tomcat/webapps/docker.war
22-Oct-2018 05:09:14.347 WARNING [localhost-startStop-1] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [4.0]. Default version will be used.
22-Oct-2018 05:09:14.742 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive /usr/local/tomcat/webapps/docker.war has finished in 1,075 ms
从34行开始就可以看到已经在部署docker.war了
浏览器访问:`http://
因为是用来练习docker中部署war, 所以我的docker.war 很简单, 就是用idea 新建出来的project 什么后台代码都没加, 只是改了index.jsp的内容。
我的web程序结构:
——完——