Search code examples
javadockertomcatamazon-ec2servlets

Docker Tomcat Container giving 404 while running servlet


I am getting this error when I run my Docker container built using my application's war file:

HTTP Status 404 – Not Found

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

The URL I am using is http://<public DNS of my linux machine>:8085/.

http://ec2-18-188-198-18.us-east-2.compute.amazonaws.com:8085

The linux machine is an AWS EC2 instance created from a linux AMI

I am running docker from this machine.

These are my docker container port details i got by using docker ps enter image description here

This is my Dockerfile

FROM tomcat:9
COPY app.war /usr/local/tomcat/webapps/
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]

Here is one of my servlet files as a reference to the kind of java code that is being used in rest of the files

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/")
public class AdminLogin extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    

As you can see I am using javax.servlets.* in my code rather than jakarta.servlets.*

Normally this problem should occur if I would have used tomcat 10 but I am using tomcat 9 with javax.servlets.*.

So I don't know how to resolve this problem.


Solution

  • I was able to solve this problem by making changes to the Dockerfile and the URL being hit.

    This is the new Dockerfile

    FROM tomcat:9.0-jdk11
    RUN cp -r $CATALINA_HOME/webapps.dist/* $CATALINA_HOME/webapps
    COPY app.war /usr/local/tomcat/webapps/
    EXPOSE 8080
    CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
    

    This is the new URL: http://<public DNS of my linux machine>:8085/<name of war file>

    http://ec2-18-188-198-18.us-east-2.compute.amazonaws.com:8085/app