Skip to the page

Containers in depth · core

Container internals and clean shutdown

A container is a Linux process with namespaces and cgroup limits. Because it is a process, signals matter: if PID 1 ignores SIGTERM, every deploy ends with a hard kill and errors.

The notes

What you must know

  • A container is an ordinary Linux process with an isolated view (namespaces) and resource limits (cgroups), started from an image. A VM runs its own kernel on virtual hardware.
  • Namespaces include pid, net, mnt, uts, ipc, user, cgroup and time. cgroups (v2 today) limit CPU, memory, I/O and process count. Going over the memory limit gets the process OOM-killed.
  • All containers share the host kernel: fast start, little overhead, weaker isolation. Use sandboxes or VMs for untrusted code.
  • Docker is the user tool, containerd manages containers, and runc creates the process. Kubernetes talks to containerd through CRI.
  • Stop sequence: SIGTERM to PID 1, wait for the grace period (docker stop 10 seconds, Kubernetes 30 seconds by default), then SIGKILL. Exit code 143 means 128 + 15 (SIGTERM). 137 means 128 + 9 (killed).
  • Pods that take exactly the grace period and exit with 137 ignored SIGTERM or never received it.
  • A shell script entrypoint that starts the app as a child without exec keeps bash as PID 1. It does not forward SIGTERM. Fix: use exec in the script, use the exec form of ENTRYPOINT, or use a small init such as tini.
  • Also handle shutdown in the app: stop accepting new work, finish requests in flight, and in Kubernetes use a readiness change and a short preStop delay so load balancers stop sending traffic first.

The red pen

Common traps: mistake → fix

  • Mistake: A container is a small VM

    Fix: it is a process on the shared kernel.

  • Mistake: kill sends the signal to the app inside the script

    Fix: it goes to PID 1, which may be the shell.

  • Mistake: Longer grace period fixes the 502 burst

    Fix: the app must handle SIGTERM.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is a container? How is it different from a VM?
  2. What do namespaces and cgroups do?
  3. Where do Docker, containerd and runc fit?
  4. Every deploy causes a burst of 502 errors and old pods stop at exactly 30 seconds. Why?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview