App in a restart loop: can't reach the database
Easy

Problem

The app container never settles — docker ps keeps flipping it between Restarting and gone. The database db next to it is up and healthy. The app was deployed minutes ago and has already restarted more than a dozen times. Find out why it can't stay up and get it running.

Initial setup

  • appmyapp:v1.2.3, Restarting, restarted many times.
  • dbpostgres:15, Up, healthy, listening on 5432.

Example interaction

$ docker ps -a
CONTAINER ID   IMAGE          STATUS                     NAMES
cafef00d1234   myapp:v1.2.3   Restarting (1) ...         app
0d1e2f3a4b5c   postgres:15    Up 8 minutes               db

$ docker inspect -f '{{.RestartCount}}' app
14
$ docker logs app
psycopg2.OperationalError: could not translate host name "postgres" to
address: Name or service not known

Acceptance

You've solved it when:

  • You've established this is a restart loop driven by a bad DB config:
a high/climbing RestartCount, and logs that show the app failing to reach its database at host postgres — but the actual database container is named db. The DB is healthy; the app's DATABASE_URL points at the wrong host.
  • You've got app running by pointing it at the real database host
(db) — e.g. recreate it with a corrected DATABASE_URL.

Constraints

  • Tools: docker CLI only.
  • End state: app in status == running, pointed at the db host.

Follow-up

  1. Why is RestartCount (not just the latest STATUS) the signal that
tells a crash loop apart from a one-time crash?
  1. The DB host in DATABASE_URL is postgres, the container is db
what two ways (rename / network alias / fix the env) resolve that, and which is least surprising?
  1. Why can a too-eager restart policy turn a simple config typo into a
noisy, CPU-burning loop that hides the root cause?
Live session
Code
SavedNo commands yet