Sub topic 16
Putting it together: a debugging checklist
This chapter assumes you know basic Docker usage, a Linux shell, process signals, and how ENTRYPOINT and CMD work. It ties the earlier tools into one repeatable order of work, so you spend less time guessing.
Why use a fixed order
Most container problems fall into a few classes: the process crashed, the kernel killed it, a health check failed, the network is wrong, or a mount is wrong. Cheap, read-only commands can separate these classes. Run those first. Only then change anything.
The checklist
- Find the container and its state. List stopped containers too, because a failed container is not shown by plain
docker ps. - Read the exit facts. Check the exit code,
OOMKilled, the restart count, and the daemon error in one command. - Read the logs. Look at the last lines first, then follow new output.
- Classify the failure. Use the exit code. 137 means SIGKILL, often an out-of-memory kill. 143 means SIGTERM. 126 and 127 mean the command could not be run or was not found.
- Look inside. Use
docker execfor a running container. Use an overridden entrypoint for one that exits at startup. - Check the environment around it. Look at health status, resource use, network, and mounts.
docker ps -a --filter status=exited --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} error={{.State.Error}}' web
docker logs --timestamps --tail 100 webMatch the symptom to the next step
| Symptom | Likely cause | Next command |
|---|---|---|
| Exit 127 | Command or binary not found | `docker run --rm -it --entrypoint sh myimage:latest` |
| Exit 137 and `oom=true` | Memory limit reached | `docker stats --no-stream` |
| Restart count keeps rising | Crash loop | `docker logs --tail 100 web`, `docker events` |
| Status `unhealthy` | Probe failing | `docker inspect --format '{{json .State.Health}}' web` |
| Running but unreachable | Port, bind address, or network | `docker run --rm -it --pid=container:web --network=container:web nicolaka/netshoot` |
| Missing or unwritable files | Mount or permission mismatch | `docker exec web ls -l /data` |
Common mistakes
- Reading only the last log line. A crash often follows an earlier warning.
- Assuming 137 always means out of memory. Check
OOMKilled. A manualdocker killor a stop that timed out also produces 137. - Fixing the container instead of the image. Changes made through
docker execvanish when the container is recreated. Put the fix in the Dockerfile or Compose file. - Debugging only one container in a Compose project. Run
docker compose ps -aanddocker compose configto see all services and the final configuration.
A worked pass
Suppose web shows Restarting (1). Inspect gives exit=1 oom=false restarts=14. The logs end with a missing environment variable. Run docker exec web env if it stays up long enough. Otherwise start the image with --entrypoint sh, and check the variable there. Add it to the Compose file, then confirm with docker compose config.
Exercise
A container named api was killed with exit code 137. docker inspect shows oom=false. What should you check next, and why?
Show answer
Exit 137 means the process received SIGKILL, but not necessarily from the out-of-memory killer. Check docker events --filter container=api for a kill or die event, and check whether someone ran docker kill or a stop exceeded its timeout and was forced. Also read the logs for a shutdown that did not finish in time.
Exercise
Why should you avoid --rm while debugging a container that exits at startup?
Show answer
With --rm, Docker deletes the container as soon as it exits. You lose its logs, inspect data, and filesystem, so you cannot run docker logs, docker inspect, docker cp, or docker diff on it.