Docker Compose Services Fail to Connect Due to Wrong Names
Your Docker Compose setup generated by Claude Code has services that fail to connect to each other. The application container can't reach the database, the backend can't connect to Redis, or the worker can't find the message broker. Everything works when running services individually but fails in Docker Compose.
Docker Compose creates a network where services can reach each other by their service name. When the application code uses 'localhost' or hardcoded IPs instead of service names, or when service names in docker-compose.yml don't match what the application expects, connections fail.
This is especially frustrating because the error messages often just say 'connection refused' or 'host not found' without indicating that the issue is a name mismatch.
Error Messages You Might See
Common Causes
- Using localhost instead of service name — Application code connects to localhost:5432 but the database is in a separate container reachable by its service name
- Service name mismatch — docker-compose.yml defines the service as 'postgres' but the app config references 'db' or 'database'
- Missing depends_on — The app container starts before the database is ready to accept connections
- Wrong port mapping confusion — Using the host-mapped port (5433) instead of the container's internal port (5432)
- Custom network not shared — Services in different docker-compose files or custom networks can't see each other
How to Fix It
- Replace localhost with service names — Change database URLs from localhost:5432 to postgres:5432 (matching the service name in docker-compose.yml)
- Use environment variables for hostnames — Define DB_HOST, REDIS_HOST as environment variables in docker-compose.yml pointing to the correct service names
- Add proper health checks — Use healthcheck in docker-compose.yml and depends_on with condition: service_healthy to wait for dependencies
- Use internal container ports — Between containers, always use the internal port (5432), not the host-mapped port (5433:5432)
- Verify with docker compose exec — Shell into a container and try to ping or connect to the service name to debug networking
Real developers can help you.
You don't need to be technical. Just describe what's wrong and a verified developer will handle the rest.
Get HelpFrequently Asked Questions
Why can't my app container connect to the database using localhost?
In Docker Compose, each service runs in its own container with its own network namespace. localhost refers to the container itself, not other containers. Use the service name defined in docker-compose.yml (e.g., 'postgres') as the hostname.
How do I wait for the database to be ready before starting my app?
Use depends_on with a health check. Define a healthcheck on the database service (e.g., pg_isready) and set depends_on: postgres: condition: service_healthy on your app service.