How to Fix Docker 404 Not Found on AWS EC2
The Root Cause
This error frequently indicates that an HTTP request successfully reached the Docker container running on your EC2 instance, but the web application within that container could not locate the requested resource at the specified URL path. On AWS EC2, while security groups and port mappings facilitate connectivity, a 404 specifically points to an internal application configuration issue, such as incorrect routing or an application not serving content from the expected root directory.
Quick Fix (CLI)
# 1. List running containers to identify the problematic one
docker ps
# 2. Inspect the container's port mappings and internal configuration (if accessible)
docker port <container_id_or_name>
docker inspect <container_id_or_name> | grep -i "Ports\|IPAddress"
# 3. If port mapping is incorrect, stop and remove the existing container
# (Replace <container_id_or_name> with your container's ID or name)
docker stop <container_id_or_name>
docker rm <container_id_or_name>
# 4. Re-run the container, ensuring correct host-to-container port mapping
# Example: Map host port 80 to container port 80. If your app listens on 3000, use 80:3000.
# Replace 'my-application-image:latest' with your actual Docker image name and tag.
docker run -d -p 80:80 --name my-application my-application-image:latest
# 5. Check the application logs for startup errors or indications of why a 404 might occur
docker logs <container_id_or_name>
Configuration Check
The primary file to inspect and potentially modify is your Dockerfile, or, if applicable, the specific web server configuration file (e.g., nginx.conf) that your Docker image uses internally.
Dockerfile Review:
EXPOSEInstruction: Verify that theEXPOSEinstruction within your Dockerfile matches the port your application actually listens on inside the container.# Example: If your application listens on port 8080 EXPOSE 8080CMDorENTRYPOINT: Ensure these instructions correctly start your application and that the application is configured to serve content from the root path (/) or the specific path you are trying to access. If using a web server like Nginx, confirm your web server’s configuration points to the correct document root.
If your application uses a framework (e.g., Node.js with Express, Python with Flask), review its routing definitions and static file serving configurations to ensure the requested path is handled.# Example for an Nginx container serving static files: # Ensure your custom nginx.conf is copied and correctly configured for the root path COPY nginx.conf /etc/nginx/conf.d/default.conf # Ensure your application files are copied to the directory Nginx expects for the root COPY ./build /usr/share/nginx/html CMD ["nginx", "-g", "daemon off;"]
Verification
After applying the fixes (and rebuilding/rerunning your container if the Dockerfile was changed):
# If you modified the Dockerfile, rebuild your image
# docker build -t my-application-image:latest .
# Then stop and remove the old container, and run the new one as in the Quick Fix steps.
# 1. From your EC2 instance's shell, attempt to access the application locally:
curl http://localhost/
# 2. From your local machine, use the EC2 instance's Public IP or Public DNS Name:
curl http://<EC2_PUBLIC_IP_OR_DNS_NAME>/
# If successful, you should receive a 200 OK response with content, rather than a 404.