Docker Swarm部署栈后端口无法访问问题求助
Let's work through this port access problem step by step. Your ip2flag_country-service container is running, but you can't reach port 8080—here are the critical checks to identify and fix the issue:
Verify port publishing in your docker-compose.yml
In Docker Swarm, double-check that your service has a proper port mapping configured. It should look something like this in your compose file:services: country-service: image: tons/ip2country:latest ports: - "8080:8080" # Maps host port 8080 to container port 8080If you're using Swarm-specific
deploysettings, ensure you haven't accidentally overridden port publishing rules that would block external access.Check Swarm service port status
Rundocker service lsand look at thePORTScolumn for yourip2flag_country-service. You should see an entry like0.0.0.0:8080->8080/tcp—if this is missing, the port isn't being published to the host network. If that's the case, re-deploy your stack after fixing the port mapping in the compose file.Confirm the container's service is listening on the right interface
Even if the container shows as "Up", the Java app might be listening only on127.0.0.1(localhost inside the container) instead of0.0.0.0(all interfaces). Run this command to check:docker exec f582b3e8d33e netstat -tulpn | grep 8080If the output shows
127.0.0.1:8080, adjust your Java app's startup command (check theCMDin your Dockerfile or the command in your compose file) to bind to0.0.0.0.Check host firewall/security group rules
If you're running this on a cloud server or a machine with a firewall, make sure port 8080 is open for inbound traffic. For example, on Ubuntu you'd runsudo ufw allow 8080/tcp, and on cloud platforms like AWS/Azure/GCP, update the security group to allow incoming connections on 8080.Check service logs for startup errors
Your container might be "Up" but the Java service failed to start properly. Pull the service logs to look for issues:docker service logs ip2flag_country-serviceWatch for errors like port conflicts, missing dependencies, or configuration issues that would prevent the app from listening on 8080.
Confirm you're accessing the correct node
If you're running a multi-node Swarm cluster, the service might be scheduled on a different node than the one you're trying to access. Rundocker service ps ip2flag_country-serviceto see which node the task is running on, then try accessing that node's IP on port 8080. (Docker's routing mesh should route traffic to the correct node by default, but misconfiguration can break this.)
内容的提问来源于stack exchange,提问作者user672009

