SnowOps Night Shift

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

  1. docker logs --tail 5 returns-api

    The API logs one line per failed request with the reason. Read the host name and the error after it.

  2. docker exec returns-api env

    The environment the API process was started with. Settings such as the database host come from here and are fixed when the container is created.

  3. docker ps

    Both containers should be Up. A container that is Up can still fail every request: this only says the process is running.

  4. 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.

  5. curl -sS http://localhost:18082/returns

    One 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 resolve

Root 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:1

How 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