一、什么是Tomcat❔
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为比较流行的Web 应用服务器。
二、Docker下安装Tomcat操作演示🎨
1️⃣前期准备
#检查docker是否安装完成。 [root@docker ~]# docker version Client: Docker Engine - Community Version: 20.10.14 API version: 1.41 Go version: go1.16.15 Git commit: a224086 Built: Thu Mar 24 01:49:57 2022 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.14 API version: 1.41 (minimum version 1.12) Go version: go1.16.15 Git commit: 87a90dc Built: Thu Mar 24 01:48:24 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.5.11 GitCommit: 3df54a852345ae127d1fa3092b95168e4a88e2f8 runc: Version: 1.0.3 GitCommit: v1.0.3-0-gf46b6ba docker-init: Version: 0.19.0 GitCommit: de40ad0 [root@docker ~]# # 检查docker是否启动 systemctl status docker
2️⃣下载tomcat镜像
# 搜索tomcat镜像 [root@docker ~]# docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 3317 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 96 [OK] bitnami/tomcat Bitnami Tomcat Docker Image 44 [OK] aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 12 [OK] arm32v7/tomcat Apache Tomcat is an open source implementati… 11 arm64v8/tomcat Apache Tomcat is an open source implementati… 7 rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl… 7 [OK] maluuba/tomcat7-java8 Tomcat7 with java8. 6 amd64/tomcat Apache Tomcat is an open source implementati… 4 tomcat2111/pisignage-server PiSignage Server 3 [OK] jelastic/tomcat An image of the Tomcat Java application serv… 3 cfje/tomcat-resource Tomcat Concourse Resource 2 oobsri/tomcat8 Testing CI Jobs with different names. 2 ppc64le/tomcat Apache Tomcat is an open source implementati… 1 tomcatling/jupyterhub_aws 1 appsvc/tomcat 1 chenyufeng/tomcat-centos tomcat基于centos6的镜像 1 [OK] s390x/tomcat Apache Tomcat is an open source implementati… 0 tomcatengineering/pg_backup_rotated Clone of martianrock/pg_backup_rotated but w… 0 secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK] tomcat2111/papercut-mf PaperCut MF Application Server 0 softwareplant/tomcat Tomcat images for jira-cloud testing 0 [OK] tomcat2111/phpredisadmin This is a Docker image for phpredisadmin 0 [OK] tomcat0823/auto1 0 tomcat2111/bitbucket-pipelines-elasticsearch Elasticsearch for Bitbucket's Pipelines 0 [root@docker ~]# # 默认下载官方最新tomcat镜像 [root@docker ~]# docker pull tomcat Using default tag: latest latest: Pulling from library/tomcat 0e29546d541c: Pull complete 9b829c73b52b: Pull complete cb5b7ae36172: Pull complete 6494e4811622: Pull complete 668f6fcc5fa5: Pull complete dc120c3e0290: Pull complete 8f7c0eebb7b1: Pull complete 77b694f83996: Pull complete 0f611256ec3a: Pull complete 4f25def12f23: Pull complete Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324 Status: Downloaded newer image for tomcat:latest docker.io/library/tomcat:latest # 查看下载的tomcat镜像 [root@docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest fb5657adc892 4 months ago 680MB [root@docker ~]# # 查看tomcat镜像详细信息 [root@docker ~]# docker inspect fb5657adc892 [ { "Id": "sha256:fb5657adc892ed15910445588404c798b57f741e9921ff3c1f1abe01dbb56906", "RepoTags": [ "tomcat:latest" ], "RepoDigests": [ "tomcat@sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324" ], "Parent": "", "Comment": "", "Created": "2021-12-22T17:07:13.333084424Z", "Container": "de0900b3a6caf902ccdaa1c7871d244e29978119ad8a1cce799cf47f1717b679", "ContainerConfig": { "Hostname": "de0900b3a6ca", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "8080/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "JAVA_HOME=/usr/local/openjdk-11", "LANG=C.UTF-8", "JAVA_VERSION=11.0.13", "CATALINA_HOME=/usr/local/tomcat", "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib", "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib", "GPG_KEYS=A9C5DF4D22E99998D9875A5110C01C5A2F6059E7", "TOMCAT_MAJOR=10", "TOMCAT_VERSION=10.0.14", "TOMCAT_SHA512=c2d2ad5ed17f7284e3aac5415774a8ef35434f14dbd9a87bc7230d8bfdbe9aa1258b97a59fa5c4030e4c973e4d93d29d20e40b6254347dbb66fae269ff4a61a5" ], "Cmd": [ "/bin/sh", "-c", "#(nop) ", "CMD [\"catalina.sh\" \"run\"]" ], "Image": "sha256:6e2683bf6f13f0050833b6807871b4980142835747139a2c2ae91b274787e399", "Volumes": null, "WorkingDir": "/usr/local/tomcat", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "DockerVersion": "20.10.7", "Author": "", "Config": { "Hostname": "", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "8080/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "JAVA_HOME=/usr/local/openjdk-11", "LANG=C.UTF-8", "JAVA_VERSION=11.0.13", "CATALINA_HOME=/usr/local/tomcat", "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib", "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib", "GPG_KEYS=A9C5DF4D22E99998D9875A5110C01C5A2F6059E7", "TOMCAT_MAJOR=10", "TOMCAT_VERSION=10.0.14", "TOMCAT_SHA512=c2d2ad5ed17f7284e3aac5415774a8ef35434f14dbd9a87bc7230d8bfdbe9aa1258b97a59fa5c4030e4c973e4d93d29d20e40b6254347dbb66fae269ff4a61a5" ], "Cmd": [ "catalina.sh", "run" ], "Image": "sha256:6e2683bf6f13f0050833b6807871b4980142835747139a2c2ae91b274787e399", "Volumes": null, "WorkingDir": "/usr/local/tomcat", "Entrypoint": null, "OnBuild": null, "Labels": null }, "Architecture": "amd64", "Os": "linux", "Size": 679618222, "VirtualSize": 679618222, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/17750c528974cc2506240a6fc097828a654b5c95254d04351888cc7c37616e8d/diff:/var/lib/docker/overlay2/2baff1b0a90a8a32b6096919dfe7905e80a10644a0e591a235bbf9dfb1ee935f/diff:/var/lib/docker/overlay2/57e4f3246f56478dec6001d04e2a194482af11bbe0cf1fff9bf6b8e80692d80b/diff:/var/lib/docker/overlay2/9c61f3a83975026b30fe67c366749cdc94dce6f8631bdf682b30c241738f5877/diff:/var/lib/docker/overlay2/2cce0104900d2801c26a4fc435b373956d73f7c879f577fe4dee76c5b4e5edf6/diff:/var/lib/docker/overlay2/20e1755fe858a6361dd98e0442356614957f69771bef40fede9ba92fd1f4fe8c/diff:/var/lib/docker/overlay2/6b2303e71edfa4940ad9eb735327bb7dff5a9c1a1f64aab39ad4be0010e8cdc1/diff:/var/lib/docker/overlay2/4fd1c7225ffdf29892025b5bbcffd78c7c30354d645ce060e9dbed6aacd76509/diff:/var/lib/docker/overlay2/7f98cf673a67095f92f9811fa0a1b1433494d4e643bf5ea64c56296d0f64827b/diff", "MergedDir": "/var/lib/docker/overlay2/63e3094912356d6fa8ae806060288efe002b8fd505d7c8c13be6b007c23ed74d/merged", "UpperDir": "/var/lib/docker/overlay2/63e3094912356d6fa8ae806060288efe002b8fd505d7c8c13be6b007c23ed74d/diff", "WorkDir": "/var/lib/docker/overlay2/63e3094912356d6fa8ae806060288efe002b8fd505d7c8c13be6b007c23ed74d/work" }, "Name": "overlay2" }, "RootFS": { "Type": "layers", "Layers": [ "sha256:11936051f93baf5a4fb090a8fa0999309b8173556f7826598e235e8a82127bce", "sha256:31892cc314cb1993ba1b8eb5f3002c4e9f099a9237af0d03d1893c6fcc559aab", "sha256:8bf42db0de72f74f4ef0c1d1743f5d54efc3491ee38f4af6d914a6032148b78e", "sha256:26a504e63be4c63395f216d70b1b8af52263a5289908df8e96a0e7c840813adc", "sha256:f9e18e59a5651609a1503ac17dcfc05856b5bea21e41595828471f02ad56a225", "sha256:832e177bb5008934e2f5ed723247c04e1dd220d59a90ce32000b7c22bd9d9b54", "sha256:3bb5258f46d2a511ddca2a4ec8f9091d676a116830a7f336815f02c4b34dbb23", "sha256:59c516e5b6fafa2e6b63d76492702371ca008ade6e37d931089fe368385041a0", "sha256:bd2befca2f7ef51f03b757caab549cc040a36143f3b7e3dab94fb308322f2953", "sha256:3e2ed6847c7a081bd90ab8805efcb39a2933a807627eb7a4016728f881430f5f" ] }, "Metadata": { "LastTagTime": "0001-01-01T00:00:00Z" } } ] [root@docker ~]#
3️⃣启动tomcat
# 启动tomcat [root@docker ~]# docker run -d -p 3366:8080 --name tomcat-xybdiy tomcat c0b92c78776bdeea0cad131b36e58931e0ffc4b59d08751e8a05a4a3becff92e ############################### # 参数解释: # -p : 宿主机端口:容器内部端口 # -d : 代表后台运行 # --name : 给容器命名 ############################### # 查看容器启动状态信息 [root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c0b92c78776b tomcat "catalina.sh run" 2 minutes ago Up 2 minutes 0.0.0.0:3366->8080/tcp, :::3366->8080/tcp tomcat-xybdiy # 查看tomcat容器详细信息 [root@docker ~]# docker inspect tomcat-xybdiy [ { "Id": "c0b92c78776bdeea0cad131b36e58931e0ffc4b59d08751e8a05a4a3becff92e", "Created": "2022-05-06T15:58:32.234116782Z", "Path": "catalina.sh", "Args": [ "run" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 12728, "ExitCode": 0, "Error": "", "StartedAt": "2022-05-06T15:58:32.992454668Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:fb5657adc892ed15910445588404c798b57f741e9921ff3c1f1abe01dbb56906", "ResolvConfPath": "/var/lib/docker/containers/c0b92c78776bdeea0cad131b36e58931e0ffc4b59d08751e8a05a4a3becff92e/resolv.conf", "HostnamePath": "/var/lib/docker/containers/c0b92c78776bdeea0cad131b36e58931e0ffc4b59d08751e8a05a4a3becff92e/hostname", "HostsPath": "/var/lib/docker/containers/c0b92c78776bdeea0cad131b36e58931e0ffc4b59d08751e8a05a4a3becff92e/hosts", "LogPath": "/var/lib/docker/containers/c0b92c78776bdeea0cad131b36e58931e0ffc4b59d08751e8a05a4a3becff92e/c0b92c78776bdeea0cad131b36e58931e0ffc4b59d08751e8a05a4a3becff92e-json.log", "Name": "/tomcat-xybdiy", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": { "8080/tcp": [ { "HostIp": "", "HostPort": "3366" } ] }, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "host", "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/5fe647bac2e4fbc4c944260bb8c177c05165c53cf170ff4c2cb07b1e589db435-init/diff:/var/lib/docker/overlay2/63e3094912356d6fa8ae806060288efe002b8fd505d7c8c13be6b007c23ed74d/diff:/var/lib/docker/overlay2/17750c528974cc2506240a6fc097828a654b5c95254d04351888cc7c37616e8d/diff:/var/lib/docker/overlay2/2baff1b0a90a8a32b6096919dfe7905e80a10644a0e591a235bbf9dfb1ee935f/diff:/var/lib/docker/overlay2/57e4f3246f56478dec6001d04e2a194482af11bbe0cf1fff9bf6b8e80692d80b/diff:/var/lib/docker/overlay2/9c61f3a83975026b30fe67c366749cdc94dce6f8631bdf682b30c241738f5877/diff:/var/lib/docker/overlay2/2cce0104900d2801c26a4fc435b373956d73f7c879f577fe4dee76c5b4e5edf6/diff:/var/lib/docker/overlay2/20e1755fe858a6361dd98e0442356614957f69771bef40fede9ba92fd1f4fe8c/diff:/var/lib/docker/overlay2/6b2303e71edfa4940ad9eb735327bb7dff5a9c1a1f64aab39ad4be0010e8cdc1/diff:/var/lib/docker/overlay2/4fd1c7225ffdf29892025b5bbcffd78c7c30354d645ce060e9dbed6aacd76509/diff:/var/lib/docker/overlay2/7f98cf673a67095f92f9811fa0a1b1433494d4e643bf5ea64c56296d0f64827b/diff", "MergedDir": "/var/lib/docker/overlay2/5fe647bac2e4fbc4c944260bb8c177c05165c53cf170ff4c2cb07b1e589db435/merged", "UpperDir": "/var/lib/docker/overlay2/5fe647bac2e4fbc4c944260bb8c177c05165c53cf170ff4c2cb07b1e589db435/diff", "WorkDir": "/var/lib/docker/overlay2/5fe647bac2e4fbc4c944260bb8c177c05165c53cf170ff4c2cb07b1e589db435/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "c0b92c78776b", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "8080/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-11/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "JAVA_HOME=/usr/local/openjdk-11", "LANG=C.UTF-8", "JAVA_VERSION=11.0.13", "CATALINA_HOME=/usr/local/tomcat", "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib", "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib", "GPG_KEYS=A9C5DF4D22E99998D9875A5110C01C5A2F6059E7", "TOMCAT_MAJOR=10", "TOMCAT_VERSION=10.0.14", "TOMCAT_SHA512=c2d2ad5ed17f7284e3aac5415774a8ef35434f14dbd9a87bc7230d8bfdbe9aa1258b97a59fa5c4030e4c973e4d93d29d20e40b6254347dbb66fae269ff4a61a5" ], "Cmd": [ "catalina.sh", "run" ], "Image": "tomcat", "Volumes": null, "WorkingDir": "/usr/local/tomcat", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "10cf9b877928ac69f7252b07f62bdff05bc91c4d292ce08c127b9db0f2416dbe", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "8080/tcp": [ { "HostIp": "0.0.0.0", "HostPort": "3366" }, { "HostIp": "::", "HostPort": "3366" } ] }, "SandboxKey": "/var/run/docker/netns/10cf9b877928", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "c870fe581d49fb5a1ed0ace2730c5338288a5ce805593fed578d9f384b48c93f", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "f92a9c592ee698e109ae6368be86f863f91d3ac013655a88b0c194549dd8e0fb", "EndpointID": "c870fe581d49fb5a1ed0ace2730c5338288a5ce805593fed578d9f384b48c93f", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:02", "DriverOpts": null } } } } ] [root@docker ~]#
4️⃣测试访问报错
# 测试访问 # 打开浏览器输入:IP地址:映射端口。本例为http://192.168.200.66:3366/ # 发生"HTTP状态404"报错信息,表示服务器未得到请求,没有找到请求资源。
5️⃣修改配置参数
# 查看Tomcat容器 docker ps # 进入Tomcat容器 [root@docker ~]# docker exec -it tomcat-xybdiy /bin/bash # 查看文件 root@c0b92c78776b:/usr/local/tomcat# ls BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work # 查看webapps目录 root@c0b92c78776b:/usr/local/tomcat# ls -l webapps total 0 # 查看webapps.dist目录 root@c0b92c78776b:/usr/local/tomcat# ls -l webapps.dist/ total 4 drwxr-xr-x 3 root root 223 Dec 22 17:06 ROOT drwxr-xr-x 15 root root 4096 Dec 22 17:06 docs drwxr-xr-x 7 root root 99 Dec 22 17:06 examples drwxr-xr-x 6 root root 79 Dec 22 17:06 host-manager drwxr-xr-x 6 root root 114 Dec 22 17:06 manager # 拷贝 webapps.dist目录下的文件到webapps目录下 root@c0b92c78776b:/usr/local/tomcat# cp -r webapps.dist/* webapps/ # 查看webapps目录 root@c0b92c78776b:/usr/local/tomcat# ls -l webapps total 4 drwxr-xr-x 3 root root 223 May 6 16:08 ROOT drwxr-xr-x 15 root root 4096 May 6 16:08 docs drwxr-xr-x 7 root root 99 May 6 16:08 examples drwxr-xr-x 6 root root 79 May 6 16:08 host-manager drwxr-xr-x 6 root root 114 May 6 16:08 manager root@c0b92c78776b:/usr/local/tomcat# # 退出容器 root@c0b92c78776b:/usr/local/tomcat# exit exit
6️⃣重新测试访问
测试访问成功。