Sub topic 16
Putting it together: a debugging checklist
This chapter joins the earlier techniques into one repeatable routine. It assumes you can run docker run and docker build, read a Dockerfile's ENTRYPOINT and CMD, use a Linux shell, and know that processes end when they receive signals. Each step below narrows the problem, so you never need to guess.
The shape of the workflow
Debugging a container comes down to three questions asked in order:
- What state is the container in, and how did it get there?
- What did the process itself say before it stopped or misbehaved?
- Does the cause lie in the process, the resources around it, or the environment (network, storage, other services)?
Step 1: Find the container and its state
Start with the list of all containers, including stopped ones. A plain docker ps hides exited containers, which is a common reason people think a container "vanished".
docker ps -a --filter status=exited --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'Match the state to a first move:
| State | What it implies | First move |
|---|---|---|
| created | Configured but never started | `docker inspect` for `State.Error`, then check the daemon's events |
| running | Main process is alive | Logs, then `docker exec` |
| restarting | A restart policy keeps relaunching a failing process | Logs and `RestartCount` |
| paused | Processes are frozen | `docker unpause`, then find out who paused it |
| exited | Main process ended | Exit code and logs |
| dead | Removal failed partway | Check the daemon logs and storage driver |
A container lives only as long as its main process (PID 1). "It exited" therefore means "PID 1 ended", and the exit code tells you how.
Step 2: Read the recorded facts
One inspect call answers most first questions:
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} error={{.State.Error}}' webState.Error is filled when the daemon could not start the process, for example a missing binary or a bad mount. An empty error with a non-zero exit code means the process started and then failed by itself.
Step 3: Read the logs
docker logs --timestamps --tail 100 web
docker logs --follow --since 5m webRead the end of the log first. The last lines before an exit are usually the cause. Two cautions apply:
- No output does not mean no problem. The application may log to a file instead of stdout and stderr, or the container may use a logging driver that
docker logscannot read. - A restarting container shows logs from several attempts. Use
--sinceor the timestamps to separate them.
Step 4: Decode the exit code
Codes above 128 mean "killed by signal (code minus 128)".
| Code | Meaning | Typical cause |
|---|---|---|
| 0 | Clean exit | The command finished. Check that `CMD` runs a long-lived foreground process. |
| 1 | General application error | Read the logs. |
| 126 | Found but not executable | Missing execute bit, wrong architecture, or a directory given as the command. |
| 127 | Command not found | Typo, binary missing from the image, or a script with Windows line endings. |
| 137 | SIGKILL (128+9) | The OOM killer, `docker kill`, or `docker stop` timing out. |
| 139 | SIGSEGV (128+11) | Crash in native code. |
| 143 | SIGTERM (128+15) | A normal stop request, often not an error. |
Code 137 is ambiguous. Confirm the cause with OOMKilled from Step 2. If it is true, the kernel killed the process for exceeding its memory limit. If it is false, something sent SIGKILL, and docker events can show who asked.
Step 5: Choose the branch
The container will not stay up
Reproduce the failure with a shell in place of the normal entrypoint. This lets you run the failing command by hand and see the real error.
docker run --rm -it --entrypoint sh myimage:latestCheck the files, environment variables, and file permissions the real command depends on. If the image has no sh, skip to the "no shell" branch below.
Memory pressure
docker stats --no-streamIf MEM USAGE sits near LIMIT and OOMKilled was true, either raise the limit or fix the leak. Raising the limit hides a leak, so only do it once you know the workload needs more.
Restart loops
A restart policy such as on-failure or always turns every crash into a loop, and the backoff delay grows between attempts. Inspect RestartCount, read the logs for the repeating error, and watch the daemon's view:
docker events --filter container=web --filter event=dieThe die events carry the exit code for each attempt, which shows whether the failures are identical or changing.
Running but unhealthy
docker inspect --format '{{json .State.Health}}' webThe result lists recent probe outputs. A health check defined as below fails if curl is missing from the image, even when the application is fine.
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1Plain Docker only marks the container unhealthy. It does not restart it. Something else, such as an orchestrator, must act on that status.
Running but misbehaving
docker exec -it web sh
docker exec web env
docker exec web ps auxexec starts a new process in the same namespaces. It works only on a running container, and anything you change in the container is lost when it is recreated.
No shell in the image
Attach a tooling container to the target's namespaces instead:
docker run --rm -it --pid=container:web --network=container:web nicolaka/netshootInside, ss -ltnp shows listening sockets, and curl localhost:8080 tests the service from the container's own network view. The target's filesystem is not shared, so use docker cp to fetch files.
Network symptoms
Work from the inside out:
- Does the process listen at all (
ss -ltn)? - Does it listen on
0.0.0.0rather than127.0.0.1? A server bound to loopback inside a container is unreachable from outside it. - Is the port published (
docker psshowsPORTS)? - Do both containers share a user-defined network? Name resolution between containers works only there, not on the default bridge.
Storage symptoms
Typical causes are a mount that hides files baked into the image, a user ID in the container that does not match the owner of a bind-mounted directory, and a path typo that made Docker create an empty directory on the host. Compare docker inspect mounts against what you expected, then check ls -ln inside the container.
Post-mortem
For a stopped container, you can still retrieve evidence:
docker cp web:/app/crash.log ./crash.log
docker diff webdocker diff lists files the container added (A), changed (C), or deleted (D) relative to the image.
Step 6: Compose projects
In a Compose project, check service state and the rendered configuration before suspecting the application:
docker compose ps -a
docker compose logs --tail 50 web
docker compose configconfig shows the file after variable substitution, which catches empty environment variables and wrong paths. Also check dependencies: a service can start before its database accepts connections unless depends_on uses a health condition.
A reusable first-look script
#!/bin/sh
# triage.sh: print the first facts about one container
c="$1"
[ -n "$c" ] || { echo "usage: $0 container" >&2; exit 2; }
docker ps -a --filter "name=^${c}\$" --format 'table {{.Names}}\t{{.Status}}\t{{.Image}}'
docker inspect --type container \
--format 'status={{.State.Status}} exit={{.State.ExitCode}} oom={{.State.OOMKilled}} restarts={{.RestartCount}} error={{.State.Error}}' "$c"
docker logs --timestamps --tail 50 "$c" 2>&1Common mistakes
- Changing several things at once, so you cannot tell which change fixed it.
- Running
docker rmbefore collecting the exit code and logs. - Treating exit code 137 as proof of an OOM kill without checking
OOMKilled. - Debugging a fresh
docker runthat differs from the failing one in environment, mounts, or user. - Adding debugging tools to the production image instead of attaching a separate tooling container.
Exercise
A container named api shows "Exited (137)". Which two fields from docker inspect do you check first, and how do you tell an out-of-memory kill from a manual stop?
Show answer
Check State.ExitCode and State.OOMKilled. If OOMKilled is true, the kernel killed the process for exceeding its memory limit, so look at docker stats and the memory limit. If it is false, something else sent SIGKILL, for example docker kill or a docker stop that timed out, and docker events with the die filter can show when it happened.
Exercise
A service is listening on port 8080 inside its container. You published the port, but connections from the host are reset. Name two likely causes you can check with the tooling container approach.
Show answer
The process may be bound to 127.0.0.1 instead of 0.0.0.0, which ss -ltn shows from a container sharing the network namespace. The published port may also not match the port the process listens on, which docker ps and curl localhost:8080 from the tooling container help confirm.
Exercise
A container uses the restart policy always and keeps restarting. Why can docker exec fail on it, and what do you use instead?
Show answer
docker exec needs a running container, and a restarting one is often between attempts. Use docker logs with timestamps, docker inspect for RestartCount and exit code, and docker run --entrypoint sh with the same image to reproduce the failure by hand.