Mac OS+Docker 19环境下Postman无法访问Docker容器的求助
client-service Container on Docker 19 for Mac Let’s break down how to get your container accessible step by step—this is a common gotcha with bridge networks and Spring Boot apps in Docker!
First, Verify the Basics
Start by ruling out simple issues that might be blocking access:
- Check if the container is actually running:
Rundocker-compose psand confirmclient-serviceshows a state ofUp. If it’sExitedorRestarting, there’s a problem with the service itself. - Inspect container logs for startup errors:
Rundocker-compose logs -f client-serviceto stream real-time logs. Look for:- Confirmation that Spring Boot started successfully (e.g.,
Started ClientServiceApplication in X seconds) - Errors connecting to Cassandra (since
depends_ononly waits for Cassandra to start, not to be ready—your app might fail if Cassandra isn’t fully initialized)
- Confirmation that Spring Boot started successfully (e.g.,
- Test access inside the container:
Rundocker exec -it client-service curl http://localhost:8087. If this fails, your service isn’t running properly inside the container—fix that first before troubleshooting external access.
Fix the Most Likely Culprit: Spring Boot Listening on 127.0.0.1
By default, some Spring Boot apps are configured to listen only on 127.0.0.1 (loopback inside the container). This means the service won’t respond to requests from outside the container, even with port mapping enabled.
- Update your
application-dev.yml(orapplication.properties) to force the service to listen on all interfaces:server: address: 0.0.0.0 port: 8087 - Rebuild your Docker image with the updated config:
docker build -t client-service . - Restart the container:
docker-compose down && docker-compose up -d
Validate Port Mapping and Network Setup
- Check if the port is properly exposed:
Rundocker port client-service—you should see output like8087/tcp -> 0.0.0.0:8087. This confirms the port is mapped to all interfaces on your Mac. - Remove conflicting
/etc/hostsentry:
You added127.0.0.1 host.docker.local, but Docker 19 for Mac already configureshost.docker.internalto point to the host machine. Delete that custom entry to avoid conflicts. - Test alternative access methods:
- Try
http://localhost:8087first (this should work if port mapping is correct and the service is listening on0.0.0.0) - If localhost fails, test with Docker’s internal bridge IP: Run
ifconfig docker0to get the IP (usually something like172.17.0.1), then visithttp://172.17.0.1:8087 - Check your Mac’s firewall—temporarily disable it to rule out port blocking.
- Try
Handle Cassandra Readiness Issues
Since your app depends on Cassandra, it might crash or fail to start if Cassandra isn’t fully ready when the app boots. Add a health check to your docker-compose.yml to ensure the app only starts accepting traffic once it’s healthy:
client-service: # ... your existing config healthcheck: test: ["CMD", "curl", "-f", "http://localhost:8087/actuator/health"] interval: 30s timeout: 10s retries: 3
Note: This requires adding the Spring Boot Actuator dependency to your project.
内容的提问来源于stack exchange,提问作者prostý člověk

