文章目录
- 1.寻找,拉取镜像
- 2.下载tomcat
- 3.启动Tomcat
- 4.tomcat测试页面修改为正常版
- 5.部署Tomcat项目
1.寻找,拉取镜像
最好先去github上看一下官方中tomcat都有哪些版本号,不能无中生有
。
#官方的使用
docker run -it --rm tomcat:9.0
#docker run -it --rm这个命令是用于做测试,用完立即自动删除。
Tips:在Docker容器退出时,默认容器内部的文件系统仍然被保留,以方便调试并保留用户数据。但是,对于foreground容器,由于其只是在开发调试过程中短期运行,其用户数据并无保留的必要,因而可以在容器启动时设置--rm选项,这样在容器退出时就能够自动清理容器内部的文件系统。
# 测试
#我这里用的是tomcat8.5,目的是为了跟我要做的项目中使用的tomcat版本号对应起来。
[root@VM-0-17-centos ~]# docker run -it tomcat:8.5
Unable to find image 'tomcat:8.5' locally
docker^H^H^H8.5: Pulling from library/tomcat
d6ff36c9ec48: Pull complete
c958d65b3090: Pull complete
edaf0a6b092f: Pull complete
80931cf68816: Pull complete
bf04b6bbed0c: Pull complete
41dc8052672f: Pull complete
dbbc65a7534c: Pull complete
4aa5acb4765b: Pull complete
5228783f1c50: Pull complete
fdbf0ecec6bf: Pull complete
Digest: sha256:e9106b424e91974eebdabb4bf9eb95e6846a41ef7fedf3d22f957bdcfe9c835f
Status: Downloaded newer image for tomcat:8.5
docker^H^H^H^HUsing CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/openjdk-8
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
#下面这一部分就是我们在本地编译器中启动tomcat的效果,看到8080端口,即启动成功
10-Aug-2020 13:29:00.676 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/8.5.57
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 30 2020 21:49:10 UTC
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.57.0
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-1127.13.1.el7.x86_64
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-8/jre
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_265-b01
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
10-Aug-2020 13:29:00.678 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
10-Aug-2020 13:29:00.679 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
10-Aug-2020 13:29:00.679 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
10-Aug-2020 13:29:00.679 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
10-Aug-2020 13:29:00.679 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
10-Aug-2020 13:29:00.679 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
10-Aug-2020 13:29:00.679 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
10-Aug-2020 13:29:00.679 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
10-Aug-2020 13:29:00.679 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
10-Aug-2020 13:29:00.679 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
10-Aug-2020 13:29:00.686 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.24] using APR version [1.6.5].
10-Aug-2020 13:29:00.687 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
10-Aug-2020 13:29:00.687 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
10-Aug-2020 13:29:00.694 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d 10 Sep 2019]
10-Aug-2020 13:29:00.848 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
10-Aug-2020 13:29:00.862 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
10-Aug-2020 13:29:00.887 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 1042 ms
10-Aug-2020 13:29:00.943 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
10-Aug-2020 13:29:00.944 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.5.57
10-Aug-2020 13:29:00.966 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
10-Aug-2020 13:29:01.072 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 184 ms
2.下载tomcat
#下载拉取镜像指令:
docker pull tomcat:8.5
#测试操作:
[root@VM-0-17-centos ~]# docker pull tomcat:8.5
8.5: Pulling from library/tomcat
d6ff36c9ec48: Pull complete
c958d65b3090: Pull complete
edaf0a6b092f: Pull complete
80931cf68816: Pull complete
bf04b6bbed0c: Pull complete
41dc8052672f: Pull complete
dbbc65a7534c: Pull complete
4aa5acb4765b: Pull complete
5228783f1c50: Pull complete
fdbf0ecec6bf: Pull complete
Digest: sha256:e9106b424e91974eebdabb4bf9eb95e6846a41ef7fedf3d22f957bdcfe9c835f
Status: Downloaded newer image for tomcat:8.5
docker.io/library/tomcat:8.5
3.启动Tomcat
#启动Tomcat的命令:
docker run -d -p 宿主机端口号:tomcat的端口号 --name 给这个tomcat起个名字 tomcat
#启动测试:
[root@VM-0-17-centos ~]# docker run -d -p 3456:8080 --name tomcat02 tomcat
558387759e3e07c70de5b249fa471215ea2cfad4a31f81e8c8baaf5c5901c6c2
在外网测试效果如下图(但是是404页面):
上面的页main是404的原因是:这个tomcat镜像是被阉割版的。配置了阿里云镜像加速的原因,默认下载的是最小的镜像,不必要的东西,就被删除掉了。
4.tomcat测试页面修改为正常版
针对于以上问题,让我们进入下载好的tomcat镜像中一探究竟。
#进入tomcat
[root@VM-0-17-centos ~]# docker exec -it tomcat02 /bin/bash
root@558387759e3e:/usr/local/tomcat#
#查看tomcat中的文件名
root@558387759e3e:/usr/local/tomcat# ls
BUILDING.txt NOTICE RUNNING.txt lib temp work
CONTRIBUTING.md README.md bin logs webapps
#查看tomcat中文件的详细信息
root@558387759e3e:/usr/local/tomcat# ls -al
total 172
drwxr-xr-x 1 root root 4096 Aug 5 19:22 .
drwxr-xr-x 1 root root 4096 Aug 5 19:18 ..
-rw-r--r-- 1 root root 18982 Jun 30 20:14 BUILDING.txt
-rw-r--r-- 1 root root 5409 Jun 30 20:14 CONTRIBUTING.md
-rw-r--r-- 1 root root 57092 Jun 30 20:14 LICENSE
-rw-r--r-- 1 root root 2333 Jun 30 20:14 NOTICE
-rw-r--r-- 1 root root 3255 Jun 30 20:14 README.md
-rw-r--r-- 1 root root 6898 Jun 30 20:14 RELEASE-NOTES
-rw-r--r-- 1 root root 16262 Jun 30 20:14 RUNNING.txt
drwxr-xr-x 2 root root 4096 Aug 5 19:23 bin
drwxr-xr-x 1 root root 4096 Aug 11 15:07 conf
drwxr-xr-x 2 root root 4096 Aug 5 19:22 lib
drwxrwxrwx 1 root root 4096 Aug 12 15:00 logs
drwxr-xr-x 2 root root 4096 Aug 5 19:22 native-jni-lib
drwxrwxrwx 2 root root 4096 Aug 5 19:22 temp
drwxr-xr-x 1 root root 4096 Aug 11 15:49 webapps
drwxr-xr-x 7 root root 4096 Jun 30 20:12 webapps.dist
drwxrwxrwx 1 root root 4096 Aug 11 15:43 work
#进入webapps文件夹里面,查看情况,发现webapps里面是空的,没有东西
root@558387759e3e:/usr/local/tomcat# cd webapps
root@558387759e3e:/usr/local/tomcat/webapps# ls
root@558387759e3e:/usr/local/tomcat/webapps#
#我们将tomcat下的webapps.dist中的内容拷贝到tomcat下的webapps文件夹中去。
root@558387759e3e:/usr/local/tomcat# cd webapps.dist
root@558387759e3e:/usr/local/tomcat/webapps.dist# ls
ROOT docs examples host-manager manager
root@558387759e3e:/usr/local/tomcat# cp -r webapps.dist/* webapps
#进入webapps文件夹中查看,我们可以发现,现在的webapps.dist中的东西拷贝到了webapps文件夹下
root@558387759e3e:/usr/local/tomcat# cd webapps
root@558387759e3e:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
将tomcat下面的的webapps.dist拷贝webapps中去就可以了,处理后再次在公网访问tomcat,就是如下的界面:
5.部署Tomcat项目
首先将你的tomcart项目打包成war包。打包方式网上一搜一大把,这里就不细说了。