>>>我的博客<<<
一、使用docker run命令直接启动一个tomcat容器
1.1 下载官方tomcat镜像
[root@dev nginx]# docker search tomcat
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/tomcat Apache Tomcat is an open source implementa... 1942 [OK]
docker.io docker.io/tomee Apache TomEE is an all-Apache Java EE cert... 51 [OK]
docker.io docker.io/dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 ba... 49 [OK]
docker.io docker.io/davidcaste/alpine-tomcat Apache Tomcat 7/8 using Oracle Java 7/8 wi... 27 [OK]
docker.io docker.io/bitnami/tomcat Bitnami Tomcat Docker Image 19 [OK]
docker.io docker.io/consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 16 [OK]
docker.io docker.io/cloudesire/tomcat Tomcat server, 6/7/8 15 [OK]
docker.io docker.io/tutum/tomcat Base docker image to run a Tomcat applicat... 10
docker.io docker.io/meirwa/spring-boot-tomcat-mysql-app a sample spring-boot app using tomcat and ... 9 [OK]
docker.io docker.io/jeanblanchard/tomcat Minimal Docker image with Apache Tomcat 8
docker.io docker.io/aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 7 [OK]
docker.io docker.io/rightctrl/tomcat CentOS , Oracle Java, tomcat application s... 3 [OK]
docker.io docker.io/amd64/tomcat Apache Tomcat is an open source implementa... 2
docker.io docker.io/arm64v8/tomcat Apache Tomcat is an open source implementa... 2
docker.io docker.io/fabric8/tomcat-8 Fabric8 Tomcat 8 Image 2 [OK]
docker.io docker.io/maluuba/tomcat7-java8 Tomcat7 with java8. 2
docker.io docker.io/99taxis/tomcat7 Tomcat7 1 [OK]
docker.io docker.io/camptocamp/tomcat-logback Docker image for tomcat with logback integ... 1 [OK]
docker.io docker.io/primetoninc/tomcat Apache tomcat 8.5, 8.0, 7.0 1 [OK]
docker.io docker.io/jelastic/tomcat 0
docker.io docker.io/oobsri/tomcat8 Testing CI Jobs with different names. 0
docker.io docker.io/picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAG... 0 [OK]
docker.io docker.io/s390x/tomcat Apache Tomcat is an open source implementa... 0
docker.io docker.io/swisstopo/service-print-tomcat backend tomcat for service-print "the true... 0
docker.io docker.io/trollin/tomcat 0
[root@dev nginx]# docker pull docker.io/tomcat
Using default tag: latest
Trying to pull repository docker.io/library/tomcat ...
latest: Pulling from docker.io/library/tomcat
55cbf04beb70: Pull complete
1607093a898c: Pull complete
9a8ea045c926: Pull complete
1290813abd9d: Pull complete
8a6b982ad6d7: Pull complete
abb029e68402: Pull complete
8cd067dc06dc: Pull complete
1b9ce2097b98: Pull complete
d6db5874b692: Pull complete
25b4aa3d52c5: Pull complete
53ec227dabf0: Pull complete
242938ace8b4: Pull complete
Digest: sha256:87ad70ceaafd5c71301b081b37ca2795bd6c7c1a5599a8c92c9447bbd225ae47
Status: Downloaded newer image for docker.io/tomcat:latest
[root@dev nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx_image1 latest c4d5f5accc06 57 minutes ago 109 MB
docker.io/tomcat latest 2d43521f2b1a 5 days ago 463 MB
docker.io/nginx latest 8b89e48b5f15 6 days ago 109 MB
[root@dev nginx]#
1.2 启动容器
[root@dev nginx]# docker run -p 8080:8080 docker.io/tomcat
23-Jul-2018 09:31:43.954 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version: Apache Tomcat/8.5.32
23-Jul-2018 09:31:43.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 20 2018 19:50:35 UTC
23-Jul-2018 09:31:43.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number: 8.5.32.0
23-Jul-2018 09:31:43.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
23-Jul-2018 09:31:43.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-693.2.2.el7.x86_64
23-Jul-2018 09:31:43.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
23-Jul-2018 09:31:43.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-8-openjdk-amd64/jre
23-Jul-2018 09:31:43.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_171-8u171-b11-1~deb9u1-b11
23-Jul-2018 09:31:43.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
23-Jul-2018 09:31:43.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
23-Jul-2018 09:31:43.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
23-Jul-2018 09:31:43.968 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.17] using APR version [1.5.2].
23-Jul-2018 09:31:43.968 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
23-Jul-2018 09:31:43.968 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
23-Jul-2018 09:31:43.981 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.0f 25 May 2017]
23-Jul-2018 09:31:44.152 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
23-Jul-2018 09:31:44.168 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
23-Jul-2018 09:31:44.190 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
23-Jul-2018 09:31:44.193 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
23-Jul-2018 09:31:44.197 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 987 ms
23-Jul-2018 09:31:44.247 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
23-Jul-2018 09:31:44.248 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.32
23-Jul-2018 09:31:44.281 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/docs]
23-Jul-2018 09:31:44.886 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/docs] has finished in [606] ms
23-Jul-2018 09:31:44.893 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/examples]
23-Jul-2018 09:31:45.359 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/examples] has finished in [465] ms
23-Jul-2018 09:31:45.359 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/ROOT]
23-Jul-2018 09:31:45.396 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/ROOT] has finished in [37] ms
23-Jul-2018 09:31:45.397 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/manager]
23-Jul-2018 09:31:45.454 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/manager] has finished in [56] ms
23-Jul-2018 09:31:45.454 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/usr/local/tomcat/webapps/host-manager]
23-Jul-2018 09:31:45.499 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/usr/local/tomcat/webapps/host-manager] has finished in [45] ms
23-Jul-2018 09:31:45.504 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
23-Jul-2018 09:31:45.533 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
23-Jul-2018 09:31:45.538 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 1340 ms
1.3 浏览器查看
tomcat:80801.4 部署自己的web应用
将本地war包上传到宿主机/opt/tomcat8/
目录下。
先停止原来的tomcat容器,然后重新挂在新的war包并启动。
[root@dev nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
00a88a12a6f4 nginx_image1 "/bin/sh -c 'nginx..." About an hour ago Up About an hour 0.0.0.0:80->80/tcp nginx
[root@dev nginx]# docker stop 00a88a12a6f4
00a88a12a6f4
[root@dev nginx]# docker rm 00a88a12a6f4
00a88a12a6f4
[root@dev nginx]# cd /opt/tomcat8/
[root@dev tomcat8]# ll
total 25852
-rw-r--r-- 1 root root 26470421 Jul 23 17:40 weixin.war
[root@dev tomcat8]# docker run -d -v /opt/tomcat8/weixin.war:/usr/local/tomcat/webapps/weixin.war -p 8080:8080 docker.io/tomcat
2c2e386b7d1a2635f7be28c404a7a71a0ec8fda074b22035be6d62d28b8f8a50
[root@dev tomcat8]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2c2e386b7d1a docker.io/tomcat "catalina.sh run" 15 seconds ago Up 14 seconds 0.0.0.0:8080->8080/tcp mystifying_wilson
[root@dev tomcat8]#
调用接口查看启动情况
tomcat:8080
容器正常启动。
二、使用docker file启动tomcat容器
2.1、编写docker file
在当前/opt/tomcat8/目录下创建dockerfile文件,写入如下内容:
from docker.io/tomcat:8.5.29
MAINTAINER marvin(qdhaiqiang@163.com)
COPY webapps /usr/local/tomcat/webapps
COPY logs /usr/local/tomcat/logs
COPY conf/server.xml /usr/local/tomcat/conf/server.xml
2. 2、用dockerfile 构建出镜tomcat镜像
[root@dev tomcat8]# docker build -t marvin/tomcat .
Sending build context to Docker daemon 26.48 MB
Step 1/5 : FROM docker.io/tomcat:latest
---> 2d43521f2b1a
Step 2/5 : MAINTAINER marvin(qdhaiqiang@163.com)
---> Running in de53a8b32cf8
---> fd9c7980f09e
Removing intermediate container de53a8b32cf8
Step 3/5 : COPY webapps /usr/local/tomcat/webapps
---> 010bf40a7e8d
Removing intermediate container 27feb6b70ca8
Step 4/5 : COPY logs /usr/local/tomcat/logs
---> 010946977b25
Removing intermediate container aff55ea55e30
Step 5/5 : COPY conf/server.xml /usr/local/tomcat/conf/server.xml
---> 098f61ab6f8b
Removing intermediate container 18d241c36d15
Successfully built 098f61ab6f8b
[root@dev tomcat8]#
查看新构建的镜像
[root@dev tomcat8]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
marvin/tomcat latest 098f61ab6f8b 11 seconds ago 489 MB
nginx_image1 latest c4d5f5accc06 About an hour ago 109 MB
docker.io/tomcat latest 2d43521f2b1a 5 days ago 463 MB
docker.io/nginx latest 8b89e48b5f15 6 days ago 109 MB
[root@dev tomcat8]#
2.3、 运行新构建的镜像
[root@dev tomcat8]# docker run --name tomcat8 -d -p 8080:8080 marvin/tomcat
6f189daf58807a5683a8b9d6c01b1b30ae8770045aa551535d27b641681c256a
[root@dev tomcat8]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f189daf5880 marvin/tomcat "catalina.sh run" 3 seconds ago Up 2 seconds 0.0.0.0:8080->8080/tcp tomcat8
[root@dev tomcat8]#
2.4、 查看镜像运行效果
tomcat.png2.5、如果要配置https证书的话,可将jks的证书放于conf目录下,在server中使用相对路径引用证书,配置如下:
<Connector port="8888" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true" URIEncoding="UTF-8"
clientAuth="false" sslProtocol="TLS" keystoreFile="conf/SHA256withRSA__.haier.net.jks" keystorePass="20170905"/>
或者如果是pem文件的话
<Connector port="8443" protocol="org.apache.coyote.http11.Http11AprProtocol"
maxThreads="150" SSLEnabled="true" >
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeyFile="conf/localhost-rsa-key.pem"
certificateFile="conf/localhost-rsa-cert.pem"
certificateChainFile="conf/localhost-rsa-chain.pem"
type="RSA" />
</SSLHostConfig>
</Connector>
2.6、在分布式微服务中的应用
tomcat作为web容器被广泛应用,但我们目前restful接口特别多,每个接口都需要一个tomcat来启动,为了配置隔离,一般都会把tomcat安装文件复制多遍,分别把war包部署在对应的webapp目录下,但这样造成的问题就是tomcat安装文件占用了大量的磁盘空间,且不便于统一管理。比如要修改tomcat的某个配置,那么对应的所有的tomcat目录都要修改一遍。
基于上述情况,作者设计了一种基于docker的tomcat服务化方案。
分布式应用2.7、查看tomcat日志
因为tomcat日志是随着程序运行生成的,所以不能使用add或者copy宿主机目录的形式添加到容器,目前的思路是将容器中的logs整个目录copy到宿主机,实际应用中可用定时任务执行脚本的形式解决。
[root@dev logs]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f189daf5880 marvin/tomcat "catalina.sh run" 4 hours ago Up 4 hours 0.0.0.0:8080->8080/tcp tomcat8
[root@dev logs]#
[root@dev logs]# pwd
/opt/tomcat8/logs
[root@dev logs]# ll
total 0
[root@dev logs]# docker cp 6f189daf5880:/usr/local/tomcat/logs /opt/tomcat8/
[root@dev logs]# ll
total 16
-rw-r----- 1 root root 7863 Jul 23 18:08 catalina.2018-07-23.log
-rw-r----- 1 root root 0 Jul 23 18:07 host-manager.2018-07-23.log
-rw-r----- 1 root root 930 Jul 23 18:08 localhost.2018-07-23.log
-rw-r----- 1 root root 1424 Jul 23 22:49 localhost_access_log.2018-07-23.txt
-rw-r----- 1 root root 0 Jul 23 18:07 manager.2018-07-23.log
[root@dev logs]#
2.8 另一个tomcat+jdk的例子
#tomcat8 openjdk8
FROM centos:7
MAINTAINER fengzp <fengzp@gzyitop.com>
ENV LANG en_US.UTF-8
ENV TOMCAT_VERSION 8.5.13
ENV CATALINA_HOME /opt/apache-tomcat-$TOMCAT_VERSION
ENV PATH $CATALINA_HOME/bin:$PATH
ENV JDK_VERSION 1.8.0
WORKDIR $CATALINA_HOME
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN yum -y install java-$JDK_VERSION-openjdk-devel && rm -rf /var/cache/yum/*
ENV JAVA_HOME /usr/lib/jvm/java-openjdk
RUN yum -y install wget
RUN cd /opt/ && wget "http://apache.fayea.com/tomcat/tomcat-8/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz"
RUN cd /opt/ && tar -zxf apache-tomcat-$TOMCAT_VERSION.tar.gz
RUN cd /opt/ && rm -rf apache-tomcat-$TOMCAT_VERSION.tar.gz
RUN chmod +x $CATALINA_HOME/bin/*.sh
VOLUME $CATALINA_HOME/webapps
VOLUME $CATALINA_HOME/logs
VOLUME $CATALINA_HOME/conf
EXPOSE 8080
CMD $CATALINA_HOME/bin/startup.sh && tail -F $CATALINA_HOME/logs/catalina.out
三、不适用容器自带jdk
3.1 先将下载好的jdk和tomcat上传到宿主服务器
[root@dev tomcat8]# ls
apache-tomcat-7.0.70.tar.gz jdk-7u80-linux-x64.tar.gz
3.2 dockerfile文件如下
#pull down centos image
FROM centos
MAINTAINER test@test.com
#copy jdk and tomcat into image
ADD ./apache-tomcat-7.0.70.tar.gz /root
ADD ./jdk-7u80-linux-x64.tar.gz /root
#set environment variable
ENV JAVA_HOME /root/jdk1.7.0_80
ENV PATH $JAVA_HOME/bin:$PATH
#define entry point which will be run first when the container starts up
ENTRYPOINT /root/apache-tomcat-7.0.70/bin/startup.sh && tail -F /root/apache-tomcat-7.0.70/logs/catalina.out
四、参考文章:
https://blog.csdn.net/qq_32351227/article/details/78673591
https://www.cnblogs.com/zydev/p/5804289.html
网友评论