Docker-compose with django could not translate host name db to address Name or service not known
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
The Django and Docker Compose error could not translate host name "db" to address: Name or service not known means the application tried to connect to a hostname that its current environment cannot resolve. In Compose setups, db is usually the service name for the database container, so the fix is usually about network context, startup timing, or using the wrong host value from the wrong place.
Why db Works Inside Compose but Not Everywhere
Docker Compose creates an internal DNS entry for each service name on the Compose network. If your database service is named db, other containers on the same network can usually connect to it with HOST=db.
A typical setup looks like this:
Inside the web container, the hostname db resolves through Docker’s internal DNS. On your Mac or Linux host shell, it does not. That distinction matters a lot.
Check Where Django Is Actually Running
One of the most common causes is running Django on the host machine while keeping DB_HOST=db in settings. If you launch python manage.py runserver outside the container, the host operating system has no Compose DNS entry for db.
That means:
- inside the
webcontainer,dbis correct - on the host machine, use
localhostor another host-reachable address instead
A simple environment-based settings pattern keeps that explicit:
Now the container can use DB_HOST=db, while host-based commands can override it with localhost.
depends_on Does Not Mean “Database Ready”
Another subtle problem is startup timing. Compose may start the db container before web, but that does not guarantee PostgreSQL is already accepting connections. The hostname may resolve correctly and the app can still fail during boot.
A better approach is to wait for database readiness.
With PostgreSQL, a healthcheck is a clean option:
You can then combine that with a startup script or retry loop in the Django container.
Test Name Resolution from the Web Container
When debugging, do not guess. Enter the container and test resolution directly.
If that command returns an IP address, DNS resolution is working and the problem is probably elsewhere, such as credentials or startup readiness.
You can also test PostgreSQL connectivity from the same context:
or even a raw socket check if the image includes the right tools.
Networks and Service Names Must Match Reality
The service name in the Compose file is the hostname, not the image name and not an arbitrary container name you imagined. If the service is called postgres, then db will not resolve unless you explicitly add an alias.
So verify three things:
- the database service is actually named
db - the Django service and database service are on the same Compose network
- no stale container from an older Compose file is confusing the environment
docker compose down followed by docker compose up --build is often useful when the topology has changed.
A Minimal Reliable Compose Pattern
This is a stable baseline for many Django projects:
Then keep any host-side management commands separate from container-side defaults.
Common Pitfalls
The biggest pitfall is running Django outside Docker while still expecting db to resolve.
Another common issue is assuming depends_on means PostgreSQL is ready for connections. It only controls container start order.
Typos in service names also matter. Docker only resolves the actual service name or configured alias.
Finally, stale containers and old Compose networks can keep debugging stuck much longer than necessary.
Summary
- '
dbis a Docker Compose service hostname, not a universal hostname.' - It resolves inside the right container network, but not from your host shell.
- Use environment-driven Django settings so container and host contexts can choose different database hosts.
- Do not rely on
depends_onfor readiness. Add health checks or retries. - Debug from inside the
webcontainer first so you are testing the same network context Django uses.
Related reading
- Docker-Desktop for MAC Kubernetes not creating secrets or token for serviceaccounts
- docker-exec failed cd executable file not found in PATH
- Docker-ized Consul, Zookeeper and Kafka in Amazon-ECS
- docker-machine behind corporate proxy
- Docker - How can run the psql command in the postgres container?
- Docker - Postgres and pgAdmin 4 Connection refused
- Docker - Can't access Django server
- Docker build taking too long when installing grpcio via pip

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.