docker-compose
django
database-connection-error
hostname-resolution
troubleshooting

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.

Practice system design

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:

yaml
1services:
2  web:
3    build: .
4    command: python manage.py runserver 0.0.0.0:8000
5    volumes:
6      - .:/app
7    ports:
8      - "8000:8000"
9    depends_on:
10      - db
11    environment:
12      DB_HOST: db
13      DB_NAME: appdb
14      DB_USER: appuser
15      DB_PASSWORD: secret
16
17  db:
18    image: postgres:16
19    environment:
20      POSTGRES_DB: appdb
21      POSTGRES_USER: appuser
22      POSTGRES_PASSWORD: secret

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 web container, db is correct
  • on the host machine, use localhost or another host-reachable address instead

A simple environment-based settings pattern keeps that explicit:

python
1import os
2
3DATABASES = {
4    "default": {
5        "ENGINE": "django.db.backends.postgresql",
6        "NAME": os.environ.get("DB_NAME", "appdb"),
7        "USER": os.environ.get("DB_USER", "appuser"),
8        "PASSWORD": os.environ.get("DB_PASSWORD", "secret"),
9        "HOST": os.environ.get("DB_HOST", "localhost"),
10        "PORT": os.environ.get("DB_PORT", "5432"),
11    }
12}

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:

yaml
1services:
2  db:
3    image: postgres:16
4    environment:
5      POSTGRES_DB: appdb
6      POSTGRES_USER: appuser
7      POSTGRES_PASSWORD: secret
8    healthcheck:
9      test: ["CMD-SHELL", "pg_isready -U appuser -d appdb"]
10      interval: 5s
11      timeout: 5s
12      retries: 10

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.

bash
docker compose exec web getent hosts db

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:

bash
docker compose exec web python manage.py dbshell

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:

yaml
1services:
2  web:
3    build: .
4    command: sh -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
5    environment:
6      DB_HOST: db
7      DB_NAME: appdb
8      DB_USER: appuser
9      DB_PASSWORD: secret
10    depends_on:
11      - db
12
13  db:
14    image: postgres:16
15    environment:
16      POSTGRES_DB: appdb
17      POSTGRES_USER: appuser
18      POSTGRES_PASSWORD: secret

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

  • 'db is 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_on for readiness. Add health checks or retries.
  • Debug from inside the web container first so you are testing the same network context Django uses.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design