Troubleshooting & Logs

Difficulty: Beginner

Question

A container starts and exits immediately (or keeps restarting). Walk me through how you would debug it.

Answer

Start with docker ps -a to see the exit code, then read docker logs <container> for the application's last output, and use docker inspect to check State.ExitCode, State.OOMKilled and State.Error. If the logs are empty, override the entrypoint with an interactive shell (docker run -it --entrypoint sh image) and run the start command by hand to see what fails.

Step 1: read the exit code before anything else

A container lives exactly as long as its main process (PID 1). When that process ends, the container stops, so the exit code tells you most of the story:

Exit codeUsually means
0Process finished normally. Often the CMD ran a one-off script, or a server was started in the background (for example service nginx start) so nothing stayed in the foreground
1Generic application error: uncaught exception, bad config, failed DB connection
125docker run itself failed (bad flag, port already allocated) and the container never really started
126Command found but not executable (missing chmod +x, for example)
127Command not found (typo in CMD, binary missing in a slim or Alpine image)
137Killed with SIGKILL (128 + 9): out-of-memory kill, docker kill, or docker stop timeout
139Segmentation fault (128 + 11), often a native module built for the wrong libc
143Terminated with SIGTERM (128 + 15), a normal graceful stop

For 137, check docker inspect -f '{{.State.OOMKilled}}'. If it is true, the container hit its --memory limit and you need to raise the limit or fix the leak.

Step 2: logs, inspect and events

  • docker logs --tail 100 <id> shows stdout and stderr of PID 1, even after the container has exited. If your app writes to a file instead of stdout, the logs will be empty, which is itself a clue.
  • docker inspect <id> shows the effective Cmd, Entrypoint, Env, mounts and restart count. Many "it works on my machine" bugs are a missing environment variable or a volume mounted over the app directory.
  • docker events --since 10m shows the lifecycle (create, start, die, oom, restart) and is handy when a restart policy keeps bringing the container back too fast to catch.
  • docker stats shows live CPU and memory, useful when the container dies under load rather than at startup.

Step 3: get a shell in the same environment

If the logs do not explain it, start the image with a shell instead of the real command:

  1. docker run -it --rm --entrypoint sh my-image (use bash if the image has it; distroless images have no shell at all).
  2. Run the start command manually and watch the error.
  3. Check that files, permissions and env vars are what you expect: ls -la /app, env, cat /app/config.json.

For a container that is running but misbehaving, docker exec -it <id> sh attaches a shell to it. For shell-less images, Docker Desktop's docker debug command, or a helper container sharing the target's namespaces (--network container:<id>, --pid container:<id>), lets you inspect it without changing the image.

Common root causes in real projects

  • The database is not ready yet. In Compose, depends_on only waits for the container to start unless you add a healthcheck and condition: service_healthy.
  • The app binds to localhost (127.0.0.1) inside the container, so it runs but nobody can reach it through the published port. Bind to 0.0.0.0.

- Windows CRLF line endings in entrypoint.sh produce a confusing "no such file or directory" error, because the kernel looks for an interpreter named sh .

  • Missing secrets, for example a payments service failing fast because RAZORPAY_KEY_SECRET was not passed with -e or --env-file.
Debugging a container that exits immediately. Work from cheap, read-only checks towards reproducing the failure interactively.
  1. docker ps -a: Find the container and its exit code.
  2. docker logs <id>: Read the last stdout and stderr lines from PID 1.
  3. docker inspect <id>: Check OOMKilled, Cmd, Entrypoint, Env and mounts.
  4. docker events: See die, oom and restart events in order.
  5. Run with a shell entrypoint: Start the image with sh and run the command by hand.

Code examples

Exit code, OOM flag and recent logs

# Show stopped containers with their status
docker ps -a --filter name=payments-api

# Exit code, OOM flag and any runtime error in one line
docker inspect -f 'exit={{.State.ExitCode}} oom={{.State.OOMKilled}} err={{.State.Error}}' payments-api

# Last 100 log lines with timestamps
docker logs --tail 100 -t payments-api

Exit 137 with OOMKilled=true means the kernel killed the process for exceeding the memory limit, so the fix is memory, not something you will find in the logs.

Reproduce the failure interactively

# Override the entrypoint so the container does not exit straight away
docker run -it --rm --entrypoint sh --env-file .env payments:1.4

# Inside the container: run what CMD would have run
/app $ node dist/server.js
Error: Missing env RAZORPAY_KEY_SECRET

Replacing the entrypoint with a shell keeps the container alive so you can run the real command and see the error directly.

Key points

  • A container stops when PID 1 exits, so exit code 0 can still be a bug (nothing kept running in the foreground).
  • Exit 137 is SIGKILL: check State.OOMKilled before blaming the code.
  • docker logs only shows stdout/stderr; apps that log to files appear silent.
  • Use --entrypoint sh to reproduce failures; distroless images need docker debug or a helper container instead.
  • Fast restart loops hide errors; docker events and the RestartCount in docker inspect reveal them.

Common follow-up questions

  • How would you debug a container built from a distroless image that has no shell?
  • What is the difference between exit codes 137 and 143?
  • How do you make a Compose service wait until Postgres is actually ready?
  • How do you check live CPU and memory usage of containers?

Interview tips

  • Describe a fixed order (exit code, logs, inspect, shell) rather than a list of random commands; it shows a method.
  • Decode 137 and 143 as 128 plus the signal number; interviewers like candidates who know where the numbers come from.
  • Share a real incident, such as a DB-not-ready race or a missing env var, to make the answer credible.

Concepts covered

docker logs, Exit codes, docker inspect, OOMKilled, Entrypoint override

More Docker & Containers questions