Q: What are the common reasons for containers continuously restarting, and what commands are used to list running vs all containers?
Master diagnostic guide for analyzing Docker container reboot loops, interpreting Linux process termination exit codes (0, 1, 127, 137, 143), and inspecting entrypoint failures.
🛠️ Production Runbook & Step-by-Step Resolution
Inspect Running vs. All Containers and Exit Codes
docker ps only shows running containers. Use docker ps -a to see stopped, failed, and restarting containers with their exit status.
docker ps # Lists only running containers
docker ps -a # Lists all containers (including exited and crashing)
docker inspect <container-id> --format 'ExitCode: {{.State.ExitCode}}, Error: {{.State.Error}}'
Decoding Container Exit Codes
Exit Code 0: Process finished successfully (entrypoint completed without a foreground blocking process). Exit Code 1: Application threw an unhandled exception. Exit Code 127: Command or binary not found in PATH. Exit Code 137: Process received SIGKILL (128 + 9), caused by OOMKilled or host termination. Exit Code 143: Graceful SIGTERM (128 + 15).
Troubleshooting Entrypoint Foreground Processes
A Docker container only lives as long as its primary PID 1 process. If a startup script starts a daemon in the background (e.g., nginx &) and exits, PID 1 dies and the container stops immediately.
- Use docker ps -a to inspect all containers and extract the exact ExitCode.
- Exit Code 0: entrypoint started a daemon in the background instead of foreground (no blocking PID 1).
- Exit Code 137: 128 + 9 (SIGKILL), indicating the Linux kernel OOM killer terminated the process.
- Exit Code 127: binary or shell interpreter specified in shebang (#!) was not found in image.
- Inspect container startup with docker logs --tail 100 and docker inspect.