Docker容器运行后访问http://127.0.0.1:8888出错,求排查原因
Alright, let's dig into why you're hitting an error when trying to reach http://127.0.0.1:8888. Looking at your Dockerfile, there are two main culprits here:
1. Tomcat's Default Port Doesn't Match Your Exposed Port
You've added EXPOSE 8888 to your Dockerfile, but Tomcat runs on port 8080 by default—you haven't actually configured Tomcat to listen on 8888. The EXPOSE directive only documents which ports the container uses; it doesn't change the app's internal configuration.
Fix: Update Tomcat to Listen on 8888
Modify your Dockerfile to edit Tomcat's server.xml and replace the default 8080 port with 8888. Add this line after the RUN echo ... setenv.sh command:
RUN sed -i 's/8080/8888/g' /usr/local/tomcat/conf/server.xml
This will update the HTTP connector port in Tomcat's config to match the port you exposed.
2. You're Missing Port Mapping When Running the Container
Even if Tomcat is listening on 8888 inside the container, your host machine doesn't automatically route traffic to that port. You need to explicitly map the host port to the container port using the -p flag when starting the container.
Fix: Run the Container with Port Mapping
Use this command to start your container (replace your-image-tag with the tag you used when building):
docker run -d -p 8888:8888 your-image-tag
The -p 8888:8888 part means "forward traffic from host port 8888 to container port 8888".
Bonus: Double-Check Your App Deployment
Your Dockerfile copies the war file to /usr/local/tomcat/webapps/myProject.war. This means your app will be accessible at http://127.0.0.1:8888/myProject (not the root path /). If you want it to be available at the root, rename the war file to ROOT.war instead:
ADD /target/*.war /usr/local/tomcat/webapps/ROOT.war
Once you make these changes, rebuild your image and restart the container—you should be able to access your app without issues.
内容的提问来源于stack exchange,提问作者Aaron Guilbot

