Docker Debugging and Troubleshooting

Container Exit Codes and States

Code / stateMeaningWhat to do
Exited (0)Process finished normallyExpected for one-off jobs
Exited (1)General error — app crashedCheck logs, then docker exec in
Exited (126)Command not executableVerify CMD path and permissions
Exited (127)Command not found in imageCheck CMD spelling and install path
RestartingCrash loop — process exits immediatelyRead logs for bad config or missing dependency
UnhealthyHealthcheck failingRun the healthcheck command manually inside

Key Debugging Commands

CommandWhat it tells youWhen to use it
docker ps -aAll containers with status and exit codesFirst look at what’s running or stopped
docker logs -f <container>Live stdout/stderrCrashes, errors, unexpected behaviour
docker exec -it <container> shShell inside a running containerReproduce errors, inspect files
docker inspect <container>Full config — env, mounts, network, portsMisconfiguration with no obvious log error
docker inspect --format '{{json .State}}' <container>State block only — exit code, OOMQuick state check
docker statsLive CPU, memory, network usageResource starvation under load
docker compose logs -f <service>Logs for a Compose serviceMulti-container stacks
docker network inspect <network>Containers on a network and their IPsServices cannot reach each other
docker build --no-cache --progress=plain .Full rebuild output, no cacheSuspect a stale cached layer
docker inspect <container> --format='{{.State.OOMKilled}}'Whether killed for out-of-memoryContainer dies silently under load
docker exec -it <container> nc -zv <host> <port>TCP connectivity testNetwork or DNS issues between services
docker ps -q | wc -lCount of running containersQuick sanity check

If This, Then That

  • Container keeps restarting → docker logs -f <container>, then check env vars with docker inspect.
  • App unreachable from browser → confirm port mapping in docker ps, test from inside with nc -zv localhost <port>.
  • Service cannot reach database → docker network inspect <network>, then nc -zv database 5432 by service name.
  • Build fails in CI but works locally → docker build --no-cache --progress=plain .
  • Container dies under load → docker stats, then check OOMKilled via inspect.
  • Healthcheck shows Unhealthy → read logs, exec in, run the healthcheck command manually.

Key Takeaway Start with docker ps -a and docker logs — most problems show up in the output. Reach for inspect, exec, and stats when logs are not enough.