One of the most common failures in Compose projects is “all containers have started, but services cannot access each other.” These types of problems usually center around networking, DNS, and startup sequences.
1. Three Highly Frequent Errors
connection refusedtemporary failure in name resolutionno route to host
These errors might look similar, but their root causes are completely different.
2. Check the Network Topology First
docker compose psdocker network lsdocker network inspect <network_name>Goal: Confirm whether the services are in the same compose network.
3. Service Name Resolution Rules
Within a Compose network, services should access each other via service_name:port.
Incorrect example:
- Connecting to the database B from inside container A by accessing
localhost:5432.
Correct example:
- Using
db:5432(wheredbis the service name).
4. depends_on Does Not Mean “Service Ready”
depends_on only guarantees the startup order; it does not guarantee that the database is ready to accept connections.
It is recommended to add health checks and retry logic:
services: app: depends_on: db: condition: service_healthy db: healthcheck: test: ["CMD", "pg_isready", "-U", "postgres"] interval: 5s timeout: 3s retries: 105. The Difference Between Port Mapping and In-Container Access
portsis used for “Host <-> Container” mapping.- Inter-container access does not rely on
ports; it relies on the internal network.
Many people mistakenly believe that without port mapping, containers cannot communicate with each other. This is a typical misconception.
6. Quick Diagnostic Commands
Execute inside the container:
getent hosts dbnc -vz db 5432curl -I http://api:8080These three steps can quickly determine whether the issue lies with DNS, ports, or the service itself.
7. Common Fixes
- Unify services under the same user-defined network.
- Change cross-container access addresses to service names.
- Add health checks to dependent services.
- Add a startup retry mechanism on the application side.
Summary
The essence of Compose network failures is often a “faulty mental model.” As long as you thoroughly understand that “inside a container, localhost only points to itself,” you can locate 80% of cross-access problems on the first try.
If this article helped you, please share it with others!
Some information may be outdated





