study.

Diagnose Failures with Logs and Exit Codes · Docker

Create a controlled failure and collect evidence before restarting it.

← Docker

Learn with progress and quizzes

First, understand the concepts.

Standard output and error

docker logs retrieves process output exposed by the logging driver. Logs written only to files do not automatically all appear there.

Exit codes

A nonzero exit code is a clue, not a complete diagnosis. Read application messages and execution settings together.

Smallest useful correction

Restarting a container whose command intentionally exits 7 repeats that command. Compare it with a corrected new execution instead.

Why it matters

An exit code is a clue to interpret with logs. Similar codes do not automatically imply identical application, runtime, or forced-termination causes.

Work through the reasoning

Collect evidence first

Read ps -a, the last log messages, and inspect ExitCode, OOMKilled, Error, and timestamps. This avoids repeatedly restarting before preserving evidence.

Test one hypothesis at a time

The lab’s exit 7 is intentional. Change one command and compare outcomes rather than changing the image, memory, and network simultaneously.

Learn by doing.

Use Docker Engine or Docker Desktop with Linux containers, Compose v2, a POSIX shell, and curl on a personal practice host. Clean up only the study-prefixed resources you create. Pulling images requires network access.

Run an intentionally failing command, then inspect logs and state. The first docker run failure is expected; continue with the following commands.

docker run --name study-failure busybox:1.36 sh -c 'echo missing-demo-setting >&2; exit 7' || test "$?" -eq 7
docker logs study-failure
docker inspect --format 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}}' study-failure
docker run --rm busybox:1.36 sh -c 'echo setting-ok; exit 0'

How to check your result

Check both missing-demo-setting and exit code 7. OOMKilled=false does not support diagnosing this example as an out-of-memory failure. The corrected run should print setting-ok and exit successfully.

Clean up practice resources

Run after checking the result. Confirm these resources belong to your practice run and contain no data you need to keep.

docker rm study-failure

Apply what you learned

Exit code is 137 but OOMKilled is false. Can you immediately conclude that more memory is needed?

Reveal the reasoning after trying

No. Investigate other SIGKILL paths using timestamps, host events, and stop behavior. Match logs and state instead of diagnosing resource exhaustion from one code.

Ready to check your understanding?

What happens when you repeatedly restart a container configured to exit 7?

  1. Its image fixes itself
  2. The same command can fail again
  3. Its volume is backed up automatically
  4. It must succeed
Reveal the reasoning after trying

The same command can fail again

Restarting does not correct the command. Use logs and settings to fix the cause before running again.

Official documentation · Diagnose Failures with Logs and Exit Codes