docker启动tomcat后无法访问404怎么解决

    科技2024-09-26  18

    docker启动tomcat后无法访问404

    1、先查看容器里的 tomcat 的 webapps 目录是否有文件1.1、查看 tomcat 的 CONTAINER ID1.2、进入 tomcat 容器的目录查看 2、修改目录名称

    启动带有 tomcat 的 docker 容器 docker run -p 8089:8080 tomcat:8 端口映射8089,tomcat后面的8是tag或者说是版本

    root@jt-server:~# docker run -p 8089:8080 tomcat:8 07-Oct-2020 09:42:18.009 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/8.5.58 07-Oct-2020 09:42:18.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Sep 10 2020 21:45:33 UTC 07-Oct-2020 09:42:18.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.58.0 07-Oct-2020 09:42:18.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 07-Oct-2020 09:42:18.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.4.0-48-generic 07-Oct-2020 09:42:18.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 07-Oct-2020 09:42:18.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-8/jre 07-Oct-2020 09:42:18.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_265-b01 07-Oct-2020 09:42:18.011 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation

    我的 tomcat 版本是8.5.58

    启动成功后访问,报错404 由于 docker 仓库里的 tomcat 里面的 webapps 目录是空的,文件都放到了 webapps.dist 目录下

    1、先查看容器里的 tomcat 的 webapps 目录是否有文件

    1.1、查看 tomcat 的 CONTAINER ID

    docker ps -a

    root@jt-server:/home/jt# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7163c4ba6cfa tomcat:8 "catalina.sh run" 21 seconds ago Up 20 seconds 0.0.0.0:8089->8080/tcp zealous_herschel

    1.2、进入 tomcat 容器的目录查看

    docker exec -it [CONTAINER ID] /bin/bash

    root@jt-server:/home/jt# docker exec -it 7163c4ba6cfa /bin/bash

    查看目录 ls -l

    root@7163c4ba6cfa:/usr/local/tomcat# ls -l total 152 -rw-r--r-- 1 root root 19318 Sep 10 21:50 BUILDING.txt -rw-r--r-- 1 root root 5408 Sep 10 21:50 CONTRIBUTING.md -rw-r--r-- 1 root root 57011 Sep 10 21:50 LICENSE -rw-r--r-- 1 root root 1726 Sep 10 21:50 NOTICE -rw-r--r-- 1 root root 3257 Sep 10 21:50 README.md -rw-r--r-- 1 root root 7136 Sep 10 21:50 RELEASE-NOTES -rw-r--r-- 1 root root 16262 Sep 10 21:50 RUNNING.txt drwxr-xr-x 2 root root 4096 Sep 15 22:50 bin drwxr-xr-x 1 root root 4096 Oct 7 09:42 conf drwxr-xr-x 2 root root 4096 Sep 15 22:50 lib drwxrwxrwx 1 root root 4096 Oct 7 09:42 logs drwxr-xr-x 2 root root 4096 Sep 15 22:50 native-jni-lib drwxrwxrwx 2 root root 4096 Sep 15 22:50 temp drwxr-xr-x 2 root root 4096 Sep 15 22:50 webapps drwxr-xr-x 7 root root 4096 Sep 10 21:47 webapps.dist drwxrwxrwx 2 root root 4096 Sep 10 21:46 work root@7163c4ba6cfa:/usr/local/tomcat# root@7163c4ba6cfa:/usr/local/tomcat# ls -l webapps total 0 root@7163c4ba6cfa:/usr/local/tomcat# root@7163c4ba6cfa:/usr/local/tomcat# ls -l webapps.dist/ total 20 drwxr-xr-x 3 root root 4096 Sep 15 22:50 ROOT drwxr-xr-x 15 root root 4096 Sep 15 22:50 docs drwxr-xr-x 6 root root 4096 Sep 15 22:50 examples drwxr-xr-x 5 root root 4096 Sep 15 22:50 host-manager drwxr-xr-x 6 root root 4096 Sep 15 22:50 manager

    tomcat默认欢迎页面路径文件是 webapps/ROOT/index.jsp 而现在的路径是 webapps.dist/ROOT/index.jsp 因此需要修改文件夹名称

    root@7163c4ba6cfa:/usr/local/tomcat# ls -l webapps.dist/ROOT/ total 184 -rw-r--r-- 1 root root 7136 Sep 10 21:50 RELEASE-NOTES.txt drwxr-xr-x 2 root root 4096 Sep 15 22:50 WEB-INF -rw-r--r-- 1 root root 27235 Sep 10 21:50 asf-logo-wide.svg -rw-r--r-- 1 root root 713 Sep 10 21:47 bg-button.png -rw-r--r-- 1 root root 1918 Sep 10 21:47 bg-middle.png -rw-r--r-- 1 root root 1401 Sep 10 21:47 bg-nav.png -rw-r--r-- 1 root root 3103 Sep 10 21:47 bg-upper.png -rw-r--r-- 1 root root 21630 Sep 10 21:47 favicon.ico -rw-r--r-- 1 root root 12219 Sep 10 21:50 index.jsp -rw-r--r-- 1 root root 2376 Sep 10 21:47 tomcat-power.gif -rw-r--r-- 1 root root 5581 Sep 10 21:50 tomcat.css -rw-r--r-- 1 root root 2066 Sep 10 21:47 tomcat.gif -rw-r--r-- 1 root root 5103 Sep 10 21:47 tomcat.png -rw-r--r-- 1 root root 67795 Sep 10 21:50 tomcat.svg

    2、修改目录名称

    mv webapps webapps2 mv webapps.dist webapps

    root@7163c4ba6cfa:/usr/local/tomcat# mv webapps webapps2 root@7163c4ba6cfa:/usr/local/tomcat# mv webapps.dist webapps root@7163c4ba6cfa:/usr/local/tomcat# ls -l webapps total 20 drwxr-xr-x 3 root root 4096 Sep 15 22:50 ROOT drwxr-xr-x 15 root root 4096 Sep 15 22:50 docs drwxr-xr-x 6 root root 4096 Sep 15 22:50 examples drwxr-xr-x 5 root root 4096 Sep 15 22:50 host-manager drwxr-xr-x 6 root root 4096 Sep 15 22:50 manager

    修改完成后再次访问 访问成功!

    Processed: 0.010, SQL: 8