Module Review: Container Lifecycle
[!NOTE] This module explores the core principles of Module Review: Container Lifecycle, deriving solutions from first principles and hardware constraints to build world-class, production-ready expertise.
1. Key Takeaways
- The State Machine: Containers move through
Created,Running,Paused,Stopped, andDead. - Graceful Shutdown:
docker stopsends SIGTERM, waits 10s, then sends SIGKILL.docker killsends SIGKILL immediately. - PID 1 Responsibility: PID 1 must handle signals and reap zombie processes. If your app canโt do this, use
tiniordumb-init. - Exec vs Attach:
attachconnects to the main process (PID 1) streams.execspawns a new process in the same namespace. - Logs: Docker captures stdout/stderr to JSON files on the host. Always configure log rotation to prevent disk exhaustion.
- Self-Healing: Use Restart Policies (
always,on-failure) and Healthchecks to make your system resilient to crashes and deadlocks.
2. Flashcards
Which signal does `docker stop` send first?
SIGTERM (Signal 15)
What happens if the main process (PID 1) exits?
The container stops immediately.
Why is `docker attach` dangerous in production?
Pressing `Ctrl+C` sends SIGINT to PID 1, killing the container.
Which restart policy should you use for a database?
`always` or `unless-stopped` (to ensure it survives reboots).
What is the default log driver in Docker?
`json-file` (writes logs to a file on the host).
How do you debug a running container without killing it?
Use `docker exec -it /bin/bash` to open a new shell sidecar.</div>
</div>
</div>
---
## 3. Scenario Quiz
**Scenario**: You have a critical payment service. You deploy an update, but the container keeps restarting every minute. `docker logs` shows nothing unusual before it exits.
---
## 4. Next Steps
Now that you understand the lifecycle, you need to network these containers together.
What is the most likely cause?
A. The disk is full.
B. The Healthcheck is failing.
C. The restart policy is set to `no`.