安装
# (不建议)官方提供的命令 用完即删 一般用来测试 停止后docker ps和docker ps -a都无法查到
# docker run -it --rm tomcat:9.0
# 下载
[root@liuyi home]# docker pull tomcat
Using default tag: latest
latest: Pulling from library/tomcat
0e29546d541c: Already exists
9b829c73b52b: Already exists
cb5b7ae36172: Already exists
6494e4811622: Already exists
668f6fcc5fa5: Already exists
dc120c3e0290: Already exists
8f7c0eebb7b1: Already exists
77b694f83996: Already exists
0f611256ec3a: Pull complete
4f25def12f23: Pull complete
Digest: sha256:9dee185c3b161cdfede1f5e35e8b56ebc9de88ed3a79526939701f3537a52324
Status: Downloaded newer image for tomcat:latest
docker.io/library/tomcat:latest
# 查看所有镜像
[root@liuyi home]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 605c77e624dd 4 years ago 141MB
tomcat 9.0 b8e65a4d736d 4 years ago 680MB
tomcat latest fb5657adc892 4 years ago 680MB
hello-world latest feb5d9fea6a5 4 years ago 13.3kB
centos latest 5d0da3dc9764 4 years ago 231MB
启动
# 后台启动
[root@liuyi home]# docker run -d -p 3355:8080 --name tomcat01 tomcat
d1fa45a534241cf37a5b5b45761c4a0e6aed7bb6dee47833a0ac6e3f7384cb56
# 测试访问
# 这里访问结果是404 注意 这不是没有启动成功 是因为docker提供的tomcat镜像是简易版的 很多功能都不在
[root@liuyi home]# curl localhost:3355
<!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/10.0.14</h3></body></html>[root@liuyi home]#
# 进入tomcat容器
[root@liuyi home]# docker exec -it tomcat01 /bin/bash
# 查看tomcat目录
root@d1fa45a53424:/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
# 发现404的原因 查看webapps文件夹下目录发现是空的
root@d1fa45a53424:/usr/local/tomcat# cd webapps
root@d1fa45a53424:/usr/local/tomcat/webapps#
# 返回tomcat根目录 这里可以发现存在一个 webapps.dist 的文件夹
root@d1fa45a53424:/usr/local/tomcat/webapps# cd ..
root@d1fa45a53424:/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.dist 文件夹查看内容 发现正是webapps文件夹下该有的内容
root@d1fa45a53424:/usr/local/tomcat# cd webapps.dist/
root@d1fa45a53424:/usr/local/tomcat/webapps.dist# ls
ROOT docs examples host-manager manager
# 返回上级目录
root@d1fa45a53424:/usr/local/tomcat/webapps.dist# cd ..
# 将webapps.dist下的内容拷贝到webapps文件夹下
root@d1fa45a53424:/usr/local/tomcat# cp -r webapps.dist/* webapps
# 查看webapps文件夹下内容 发现已经全部拷贝成功
root@d1fa45a53424:/usr/local/tomcat# cd webapps
root@d1fa45a53424:/usr/local/tomcat/webapps# ls
ROOT docs examples host-manager manager
# 退出容器
root@d1fa45a53424:/usr/local/tomcat/webapps# exit
exit
# 验证测试 发现不再试404
[root@liuyi home]# curl localhost:3355
# 注:这个404是阿里云镜像的原因,默认是最小的镜像,所有不必要的都被剔除掉了
浏览器访问 (http://ip:3355)

30d8a475-0bfe-4962-9210-5f78b5d1a8a5.png