Difficulty: Beginner
A container starts and exits immediately (or keeps restarting). Walk me through how you would debug it.
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.
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 code | Usually means |
|---|---|
0 | Process 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 |
1 | Generic application error: uncaught exception, bad config, failed DB connection |
125 | docker run itself failed (bad flag, port already allocated) and the container never really started |
126 | Command found but not executable (missing chmod +x, for example) |
127 | Command not found (typo in CMD, binary missing in a slim or Alpine image) |
137 | Killed with SIGKILL (128 + 9): out-of-memory kill, docker kill, or docker stop timeout |
139 | Segmentation fault (128 + 11), often a native module built for the wrong libc |
143 | Terminated 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.
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.If the logs do not explain it, start the image with a shell instead of the real command:
docker run -it --rm --entrypoint sh my-image (use bash if the image has it; distroless images have no shell at all).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.
depends_on only waits for the container to start unless you add a healthcheck and condition: service_healthy.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
.
RAZORPAY_KEY_SECRET was not passed with -e or --env-file.# 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.
# 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.
docker logs, Exit codes, docker inspect, OOMKilled, Entrypoint override