Topic 04: Container Lifecycle & Runtime State Machine

Target Duration: 2–4 minutes (~300–450 spoken words)
Focus: Pointwise verbal delivery covering container lifecycle transitions (build, run, exec, stop), nsenter child discovery, and reverse unmount teardown.


🎙️ Pointwise Spoken Speech (Word-for-Word Delivery)


📋 Step-by-Step Summary (What, How & Why)

Step What Was Done How It Works Why This Mechanism / Order
1. Build Compiled image layer stack Persisted ordered layer path list in .images/<name>/layers. Decouples image construction from container execution.
2. Run Initialized rootfs and supervisor Mounted OverlayFS union, bind-mounted /dev, mounted procfs/sysfs. Prepares complete isolated runtime sandbox before launching entrypoint.
3. Exec Attached to active container Located supervisor $\rightarrow$ found child PID 1 $\rightarrow$ attached via nsenter. Must target child PID (and not supervisor) to enter the container's PID namespace.
4. Stop Cleaned up process & mounts Sent kill signal, deleted veth, unmounted leaf mounts before root overlay. Reverse unmount prevents kernel EBUSY errors when detaching active sub-mounts.

❓ Anticipated Interview Questions & Crisp Answers

Q1: Why did you target the child PID rather than the supervisor PID when implementing exec?

Answer: The unshare supervisor process itself runs in the host PID namespace—its sole job is to supervise the container and hold its namespace descriptors. The child process created by the supervisor is the actual process running inside the container's private PID namespace. Attaching to the supervisor would attach the user to the host PID space instead of the container.

Q2: What happens if a container process crashes or terminates unexpectedly?

Answer: The supervisor process was configured with automatic child-killing (kill-child). When the container's primary process exits, the supervisor immediately receives the exit signal and instructs the kernel to send SIGKILL to all remaining child processes in that PID namespace, preventing dangling orphan processes on the host.

Q3: Why is reverse unmount order necessary when stopping a container?

Answer: Mount points in Linux form a tree hierarchy. Leaf filesystems like /proc and /sys are mounted on top of directory inodes inside the root merged/ overlay. Attempting to unmount merged/ first fails with EBUSY because active filesystem mounts are pinned to its child directories.