本文主要学习如何通过 Docker 快速部署 tomcat 服务。
准备
官方镜像:https://hub.docker.com/_/tomcat
本文主要安装最常用的 jdk8 相关版本。
安装
1、拉取镜像
docker pull tomcat:jdk8-corretto
2、查看镜像
docker images
————————————————————
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat jdk8-corretto 79b38376f3de 9 days ago 376MB
3、创建并启动容器
docker run tomcat:jdk8-corretto
如果不指定tags,则会启动:latest版本,如果:latest版本不存在则先pull下来再创建启动。
启动成功如下:
09-May-2021 08:37:39.734 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.45
09-May-2021 08:37:39.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Mar 30 2021 10:29:04 UTC
09-May-2021 08:37:39.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 9.0.45.0
09-May-2021 08:37:39.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux
09-May-2021 08:37:39.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-1160.24.1.el7.x86_64
09-May-2021 08:37:39.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64
09-May-2021 08:37:39.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/lib/jvm/java-1.8.0-amazon-corretto/jre
09-May-2021 08:37:39.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_292-b10
09-May-2021 08:37:39.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Amazon.com Inc.
09-May-2021 08:37:39.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat
09-May-2021 08:37:39.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat
09-May-2021 08:37:39.739 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
09-May-2021 08:37:39.739 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
09-May-2021 08:37:39.739 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
09-May-2021 08:37:39.739 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
09-May-2021 08:37:39.740 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
09-May-2021 08:37:39.740 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
09-May-2021 08:37:39.740 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
09-May-2021 08:37:39.740 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
09-May-2021 08:37:39.740 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
09-May-2021 08:37:39.743 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.27] using APR version [1.6.3].
09-May-2021 08:37:39.743 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
09-May-2021 08:37:39.743 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
09-May-2021 08:37:39.746 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.0.2k-fips 26 Jan 2017]
09-May-2021 08:37:40.024 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
09-May-2021 08:37:40.059 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [475] milliseconds
09-May-2021 08:37:40.088 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
09-May-2021 08:37:40.088 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.45]
09-May-2021 08:37:40.099 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
09-May-2021 08:37:40.113 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [51] milliseconds
可以看到 tomcat 版本为9.0.45,OS版本为linux 3.10.0-1160.24.1.el7.x86_64
,jdk版本为1.8,默认端口为8080。
此时直接访问IP+端口无法直接访问到tomcat默认页面。
4、Docker 宿主机与容器通信
docker run -p 80:8080 tomcat:jdk8-corretto
此时可以访问到tomcat。
通过 netstat -tulpn 命令,可以查看到8080端口已经被代理:
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8438/docker-proxy
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 992/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 979/master
tcp6 0 0 :::80 :::* LISTEN 8443/docker-proxy
tcp6 0 0 ::1:25 :::* LISTEN 979/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 789/dhclient
udp 0 0 127.0.0.1:323 0.0.0.0:* 542/chronyd
udp6 0 0 ::1:323 :::* 542/chronyd
此时tomcat是前台运行,通过ctrl+ctomcat就停止运行了。
5、后台运行 tomcat
docker run -p 80:8080 -d tomcat:jdk8-corretto
__________________________________________
95ee28812aff942e9d1e59b2f967e4462df02902cda479d997d84199a11794ad
#说明运行成功
6、查询运行的容器及相关信息
docker ps
__________________________________________
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95ee28812aff tomcat:jdk8-corretto "catalina.sh run" About a minute ago Up About a minute 0.0.0.0:80->8080/tcp, :::80->8080/tcp frosty_johnson
容器id:95ee28812aff
镜像名称:tomcat:jdk8-corretto
执行的命令:catalina.sh run
时间:大约在1分钟前创建
端口:通过80端口代理转发到容器的8080端口
容器名称:frosty_johnson
7、停止运行
docker stop 95ee28812aff
8、删除容器
docker rm 95ee28812aff #需要先stop,在rm
docker rm -f 95ee28812aff #强制删除
9、删除镜像
查看镜像
docker images
__________________________________________
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat jdk8-corretto 79b38376f3de 9 days ago 376MB
tomcat latest c0e850d7b9bb 2 weeks ago 667MB
移除镜像
docker rmi tomcat:latest
_________________________________________
Error response from daemon: conflict: unable to remove repository reference "tomcat:latest" (must force) - container c570c6bca665 is using its referenced image c0e850d7b9bb
#容器 c570c6bca665 正在使用当前镜像,无法删除
此时先移除容器,再删除镜像
docker rm c570c6bca665
docker rmi tomcat:latest
_________________________________________
Untagged: tomcat:latest
Untagged: tomcat@sha256:0509684774ac53d8b152f4a50dd92889218a8633c0feddb4b35d092e55cd215d
Deleted: sha256:c0e850d7b9bb0aa55db12991bd0fe825cdab4f5d4be72b2a94cc760bcc3aa82f
Deleted: sha256:de48848504882240b92b703da9601af0709e38a64a9a40a07109b78446aa9e95
Deleted: sha256:1a0e354e31a0ab7456bbad4f060ea6c6f7ab7b144dc74c874bf689c7e519e27c
Deleted: sha256:cd25cd758181de964bdfb328f5c630c25263557f5a46793356c93e0e369af7a4
Deleted: sha256:b824ca3e73f4984338fc3eb212702bcadd7770320c8297fc21b2d1d265146d16
Deleted: sha256:d8b838beea6f90d6556650b670930f58d16bf0007b1a7d9effe3dd1735fdfa8d
Deleted: sha256:3f61a2fb8ca663bf88786884ba5f5be3274c91ee82c1ae9bb36bb1ad1fa0b3d0
Deleted: sha256:ac996dca9c2f86e5d2744187de88cd328f12dde417a7950dff33448906228ae3
Deleted: sha256:e4ed057c6550560fb44b9146dadd5c62bfc00404ba69937c89a44e10485c9010
Deleted: sha256:985d0a2cd810a6fc7b68886aee94ee060acd63e122a1def04370dac5fa9d05f6
Deleted: sha256:e2c6ff46235709f5178ab7c1939f4fba7237ffde84e13b1582fa5f0837c1d978
移除成功
或者
docker rmi -f tomcat:latest #强制移除,不建议在线上使用
总结
本文主要介绍了 tomcat 指定版本的快速部署过程。
评论区