others-how to solve tomcat 404 problem when using docker to start tomcat?

1. Purpose

In this post, I will show you how to solve the following problem when using docker to start tomcat:

# HTTP Status 404 – Not found

**Type** Status Report

**Description** The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

the docker command:

docker run --name tomcat -p 8080:8080 -d tomcat



2. Solution

how to solve this problem? we can just go into the docker container, and see what’s inside the webapps as follows:

[root@local tomcat]# docker ps |grep tomcat
2e010bb7c25e   tomcat                                                  "/entrypoint.sh cata…"   2 minutes ago   Up 2 minutes            0.0.0.0:8686->8080/tcp, :::8686->8080/tcp                                              tomcat
[root@local tomcat]# docker exec -it tomcat /bin/bash
root@2e010bb7c25e:/usr/local/tomcat# ls -l webapps
total 0

You can see that there is nothing inside the webapps, that’s the reason you got 404 , because there are no webapps to serve your request. Then we should do as follows(Still inside the tomcat container):

root@2e010bb7c25e:/usr/local/tomcat# ls -l
total 128
drwxr-xr-x. 2 root root  4096 Aug  8 21:18 bin
-rw-r--r--. 1 root root 20123 Jul  6 13:45 BUILDING.txt
drwxr-xr-x. 1 root root    30 Aug 10 07:21 conf
-rw-r--r--. 1 root root  6210 Jul  6 13:45 CONTRIBUTING.md
drwxr-xr-x. 2 root root  4096 Aug  8 21:17 lib
-rw-r--r--. 1 root root 60393 Jul  6 13:45 LICENSE
drwxrwxrwt. 1 root root    80 Aug 10 07:21 logs
drwxr-xr-x. 2 root root   186 Aug  8 21:18 native-jni-lib
-rw-r--r--. 1 root root  2333 Jul  6 13:45 NOTICE
-rw-r--r--. 1 root root  3398 Jul  6 13:45 README.md
-rw-r--r--. 1 root root  6776 Jul  6 13:45 RELEASE-NOTES
-rw-r--r--. 1 root root 16076 Jul  6 13:45 RUNNING.txt
drwxrwxrwt. 2 root root    38 Aug  8 21:17 temp
drwxr-xr-x. 2 root root    10 Aug  8 21:17 webapps
drwxr-xr-x. 7 root root   105 Jul  6 13:45 webapps.dist
drwxrwxrwt. 2 root root    10 Jul  6 13:45 work
root@2e010bb7c25e:/usr/local/tomcat# mv webapps webapps2
root@2e010bb7c25e:/usr/local/tomcat# mv webapps.dist webapps
root@2e010bb7c25e:/usr/local/tomcat# exit
exit
[root@local tomcat]# docker stop tomcat
tomcat
[root@local tomcat]# docker start tomcat
tomcat

You can see that we have done this jobs to make it work:

  • rename the old webapps directory to webapps2, which will not be used to serve your request
  • rename the webapps.dist to webapps, then it will be used to serve your request
  • exit the tomcat docker container
  • restart the tomcat docker container

Now visit http://x.x.x.x:8080 again, got this:

[root@local tomcat]# curl -v http://localhost:8080/
* About to connect() to localhost port 8080 (#0)
*   Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Content-Type: text/html
< Content-Length: 2626

You can see that it works!

Here is the summary, the problem’s reason is that:

It turns out that all default tomcat apps have been removed from the webapps directory for all tomcat images starting with Tomcat version 7. All apps have been moved to /usr/local/tomcat/webapps.dist directory so that they are not launched by default. You can read about it here.



3. Summary

In this post, I demonstrated how to solve the 404 error when using docker to start tomcat container. That’s it, thanks for your reading.