Why can't my Docker container connect to the database container?
Containers on the same user-defined Docker network reach each other by container name. If an app is started with a database host that no container on its network has, every connection fails to resolve the name while the database is perfectly healthy. Check the app's logs for the host it tries, docker exec env for the value it was given, and docker network inspect for the names that exist. In this incident the API was redeployed with a host name copied from another environment.
What it looks like
- Both containers are Up, and the app answers every request with a 500.
- The app's logs say the database host name does not resolve.
- The database team sees no errors and no connections.
- The app was redeployed shortly before.
Commands that find the cause
docker logs --tail 5 returns-apiThe API logs one line per failed request with the reason. Read the host name and the error after it.
docker exec returns-api envThe environment the API process was started with. Settings such as the database host come from here and are fixed when the container is created.
docker psBoth containers should be Up. A container that is Up can still fail every request: this only says the process is running.
docker network inspect edge -f '{{range .Containers}}{{.Name}} {{end}}'The containers on the edge network. On a user-defined Docker network, containers reach each other by container name, so this is the list of names the API can resolve.
curl -sS http://localhost:18082/returnsOne request the way the returns form makes it. A short error body means the API is up but failing inside.
What the evidence looked like
Recorded on a real system when this incident was captured for the game.
$ docker logs --tail 5 returns-api
ERROR cannot reach database returns-postgres:5432: [Errno -2] Name does not resolve
ERROR cannot reach database returns-postgres:5432: [Errno -2] Name does not resolve
ERROR cannot reach database returns-postgres:5432: [Errno -2] Name does not resolve
ERROR cannot reach database returns-postgres:5432: [Errno -2] Name does not resolve
ERROR cannot reach database returns-postgres:5432: [Errno -2] Name does not resolveRoot cause
The returns API was redeployed with DB_HOST=returns-postgres, a name not on its network; recreating it with returns-db fixed returns.
The fix
Recreate the API container with the database host set to the database container's name:
docker rm -f returns-api && docker run -d --name returns-api --network edge -p 18082:8000 -e DB_HOST=returns-db returns-api:1How to prevent it
Keep each environment's settings in one checked-in env file, and have the app check its database host at start so a wrong value fails at deploy time.
More incident guides
- Docker container exits immediately after a config change
- Docker port mapping not working, empty reply
- Docker volume permission denied
- Docker container exit code 137, killed for memory
- CrashLoopBackOff after a config change
- Service returns 503 while every pod is Running
- OOMKilled and exit code 137
- Pods fail to start after a Secret change
- Requests fail after adding a NetworkPolicy
- Healthy pods restarted by a liveness probe
- Slow service with low CPU and no restarts
- Rollout stuck on ImagePullBackOff
- Pods stuck Pending with insufficient CPU
- Linux disk full: no space left on device
- systemd service keeps restarting after a deploy
- Address already in use: find what holds a port on Linux
- Linux name or service not known after a config change
- Too many open files in a systemd service