安裝
# (不建議)官方提供的命令 用完即刪 一般用來(lái)測(cè)試 停止后docker ps和docker ps -a都無(wú)法查到
# 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
啟動(dòng)
# 后臺(tái)啟動(dòng)
[root@liuyi home]# docker run -d -p 3355:8080 --name tomcat01 tomcat
d1fa45a534241cf37a5b5b45761c4a0e6aed7bb6dee47833a0ac6e3f7384cb56
# 測(cè)試訪問(wèn)
# 這里訪問(wèn)結(jié)果是404 注意 這不是沒(méi)有啟動(dòng)成功 是因?yàn)閐ocker提供的tomcat鏡像是簡(jiǎn)易版的 很多功能都不在
[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]#
# 進(jìn)入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
# 發(fā)現(xiàn)404的原因 查看webapps文件夾下目錄發(fā)現(xiàn)是空的
root@d1fa45a53424:/usr/local/tomcat# cd webapps
root@d1fa45a53424:/usr/local/tomcat/webapps#
# 返回tomcat根目錄 這里可以發(fā)現(xiàn)存在一個(gè) 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
# 進(jìn)入 webapps.dist 文件夾查看內(nèi)容 發(fā)現(xiàn)正是webapps文件夾下該有的內(nèi)容
root@d1fa45a53424:/usr/local/tomcat# cd webapps.dist/
root@d1fa45a53424:/usr/local/tomcat/webapps.dist# ls
ROOT docs examples host-manager manager
# 返回上級(jí)目錄
root@d1fa45a53424:/usr/local/tomcat/webapps.dist# cd ..
# 將webapps.dist下的內(nèi)容拷貝到webapps文件夾下
root@d1fa45a53424:/usr/local/tomcat# cp -r webapps.dist/* webapps
# 查看webapps文件夾下內(nèi)容 發(fā)現(xiàn)已經(jīng)全部拷貝成功
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
# 驗(yàn)證測(cè)試 發(fā)現(xiàn)不再試404
[root@liuyi home]# curl localhost:3355
# 注:這個(gè)404是阿里云鏡像的原因,默認(rèn)是最小的鏡像,所有不必要的都被剔除掉了
瀏覽器訪問(wèn) (http://ip:3355)

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