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

  1. The State Machine: Containers move through Created, Running, Paused, Stopped, and Dead.
  2. Graceful Shutdown: docker stop sends SIGTERM, waits 10s, then sends SIGKILL. docker kill sends SIGKILL immediately.
  3. PID 1 Responsibility: PID 1 must handle signals and reap zombie processes. If your app canโ€™t do this, use tini or dumb-init.
  4. Exec vs Attach: attach connects to the main process (PID 1) streams. exec spawns a new process in the same namespace.
  5. Logs: Docker captures stdout/stderr to JSON files on the host. Always configure log rotation to prevent disk exhaustion.
  6. 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.
What is the most likely cause?
A. The disk is full.
B. The Healthcheck is failing.
C. The restart policy is set to `no`.
--- ## 4. Next Steps Now that you understand the lifecycle, you need to network these containers together.