Q: You execute `docker run -d myapp`. The terminal returns a long container ID, but immediately upon checking `docker ps`, the container is completely missing. `docker ps -a` shows it exited with code 0. Why did it immediately stop if it didn't error?
A Docker container strictly lives only as long as its primary PID 1 process is running.
#Docker #Docker #L2 #Containers #Linux
🎙️ Candidate Opening & Architectural Context
""Container stability relies on clean signal handling (SIGTERM vs SIGKILL) and immutable image tagging. The interviewer is testing: Daemonizing background processes inside containers.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third.""
Advertisement
🛠️ Production Runbook & Step-by-Step Resolution
1️⃣
Production Solution & Architecture
A Docker container strictly lives only as long as its primary PID 1 process is running. If the CMD or ENTRYPOINT in the Dockerfile starts an application in the background (e.g., executing service nginx start or appending an & to a script), the script will start the daemon, successfully finish its execution, and return an exit code of 0. Because the foreground script finished, PID 1 terminates, and Docker shuts down the container, killing everything inside it. *Fix:* You must run the main process in the foreground. Use nginx -g 'daemon off;' or execute the application binary explicitly without backgrounding it.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: A Docker container strictly lives only as long as its primary PID 1 process is running.."
⚡ 60-Second Elevator Pitch Talking Points
- Immediate Triage: A Docker container strictly lives only as long as its primary PID 1 process is running.
- Run targeted verification commands before modifying configuration.
- Automate permanent guardrails (CI check, alerts, IaC policy) to prevent recurrence.
Advertisement