>_dockerfix
← back to error wikiprisma

Prisma P1001: Can't reach database server (inside Docker)

Error: P1001: Can't reach database server at `db`:`5432`
Please make sure your database server is running at `db`:`5432`.

What's happening

This almost always happens when the app container and the database container are not actually on the same Docker network, or the app's `DATABASE_URL` points at `localhost`/`127.0.0.1` instead of the database service's name. Inside Docker, `localhost` refers to the app's own container, not the host or a sibling container — Postgres isn't listening there. It can also happen if the DB container hasn't finished starting yet when the app tries to connect (no healthcheck/wait logic), or the service name in `DATABASE_URL` doesn't match the actual service name in `docker-compose.yml`.

Fix

1. In `DATABASE_URL`, use the Compose service name as the host, not `localhost`:
```
DATABASE_URL="postgresql://user:pass@db:5432/mydb"
```
2. Confirm both containers are on the same network: `docker network inspect <network>` should list both.
3. Add a healthcheck + `depends_on: condition: service_healthy` so the app waits for Postgres to accept connections, not just for the container to start:
```yaml
db:
  healthcheck:
    test: ["CMD-SHELL", "pg_isready -U user"]
    interval: 2s
    timeout: 3s
    retries: 10
app:
  depends_on:
    db:
      condition: service_healthy
```
4. From inside the app container, sanity check connectivity: `docker exec -it <app> nc -zv db 5432`.
Got a different error? Paste it here for a free diagnosis.