Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 20/ 160
01. Containerization & Docker Fundamentals

Linux Kernel Primitives: Control Groups (cgroups) & Union File Systems

Connect cgroups resource limits with union/overlay filesystems that power image layers.

Advancedcgroups · overlay3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

In plain words

Quota pressure and filesystem layering are separate failure classes with separate diagnostics and settings.

Think of it like…

A cgroup is a metered electrical panel for a workshop; overlay is a stack of transparent plans where edits go on the top sheet.

Where you’ve seen it

In practice, docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained is the concrete move for this job: connect cgroups resource limits with union/overlay filesystems that power image layers. The release is not accepted until cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths confirms the result.

Words you’ll hear

cgroups
The implementation control in docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained for connect cgroups resource limits with union/overlay filesystems that power image layers.
overlay
The result to observe with cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths, rather than infer from a successful command alone.
Limits
The production boundary exposed when allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent.

Visual diagram

Loading interactive diagram

Theory & deep dive

cgroups control path

The concrete mechanism is docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained. The observable result is cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths; if those disagree, inspect the runtime evidence before changing unrelated settings.

Connect cgroups resource limits with union/overlay filesystems that power image layers. Keep cgroups, overlay and Limits in the same deployment record so the intent and the observed state cannot drift apart.

Concrete cgroups setup

docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained

Key takeaway: Prove overlay with cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths, not with command success alone.

Limits failure boundary

CPU throttling, memory OOM kills and an exhausted overlay writable layer can all make one app unhealthy, but fixing the wrong subsystem will not restore it. Resource limits also interact with cgroup-controller availability in the host kernel. The narrow failure to design for is allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent.

Accept this compromise explicitly: Hard resource caps protect hosts but can throttle legitimate bursts; overlay supports fast packaging but performs poorly for heavy in-place database writes. The controlled check-cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths-also identifies the point where a different design is safer.

Deliberate failure test

Exercise this failure safely: allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent.

Key takeaway: Hard resource caps protect hosts but can throttle legitimate bursts; overlay supports fast packaging but performs poorly for heavy in-place database writes.

How it works, step by step

Step 1 of 4
Design

1. State the boundary

Write down why the workload needs this: CPU throttling, memory OOM kills and an exhausted overlay writable layer can all make one app unhealthy, but fixing the wrong subsystem will not restore it. Resource limits also interact with cgroup-controller availability in the host kernel.

Quota pressure and filesystem layering are separate failure classes with separate diagnostics and settings.
Key numbers at this step
Intent
cgroups

When to use it & trade-offs

What it is

cgroups v2 account and limit resources in a process hierarchy, while a union/overlay filesystem presents immutable image layers plus a writable upper layer as one path. cgroups answer “how much?”; overlay answers “which file view?” and enables copy-on-write.

Why it matters: CPU throttling, memory OOM kills and an exhausted overlay writable layer can all make one app unhealthy, but fixing the wrong subsystem will not restore it. Resource limits also interact with cgroup-controller availability in the host kernel.

When to reach for it

  • The fit is strongest for this job: connect cgroups resource limits with union/overlay filesystems that power image layers. Pair it with cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths in the runbook.
  • Production cgroups changes should make overlay observable through docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained, rather than trusting the runtime default.
  • Keep a rollback example for the case where allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent.

When to avoid or be careful

  • Do not accept command success while the system can still fail by allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent.
  • Avoid erasing Limits protection merely to dodge this cost: “Hard resource caps protect hosts but can throttle legitimate bursts; overlay supports fast packaging but performs poorly for heavy in-place database writes”.
  • Do not replace the evidence path-cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths-with a screenshot or a mutable tag.

docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained: practical choices

Explicit cgroups
Best for: Connect cgroups resource limits with union/overlay filesystems that power image layers, with a reviewed runbook.

Strengths: cgroups v2 account and limit resources in a process hierarchy, while a union/overlay filesystem presents immutable image layers plus a writable upper layer as one path. cgroups answer “how much?”; overlay answers “which file view?” and enables copy-on-write.

Watch out: Hard resource caps protect hosts but can throttle legitimate bursts; overlay supports fast packaging but performs poorly for heavy in-place database writes.

Ambient default
Best for: Short-lived overlay experiments with low blast radius.

Strengths: It can begin with docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained and fewer surrounding controls.

Watch out: It silently accepts allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent, which makes the result hard to reproduce after promotion.

Managed Limits
Best for: Teams that centralize Limits policy across many workloads.

Strengths: CPU throttling, memory OOM kills and an exhausted overlay writable layer can all make one app unhealthy, but fixing the wrong subsystem will not restore it. Resource limits also interact with cgroup-controller availability in the host kernel.

Watch out: The platform adds an ownership and debugging layer; retain cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths as the direct check.

Interview signals & numbers to know

When this topic comes up

  • docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained
  • cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths
  • Failure signature: allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent
  • Trade-off: Hard resource caps protect hosts but can throttle legitimate bursts; overlay supports fast packaging but performs poorly for heavy in-place database writes.

Benchmarks & rules of thumb

0.5 CPU -
one quota-equivalent CPU half-time
256 MiB -
memory ceiling in the example
cgroup v2 -
unified hierarchy on modern Linux

Common mistakes & follow-ups

Common interview mistakes

  • Treating a successful docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained as proof and skipping cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths.
  • Allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent
  • Optimizing cgroups without accepting that hard resource caps protect hosts but can throttle legitimate bursts; overlay supports fast packaging but performs poorly for heavy in-place database writes.

Likely interviewer follow-ups

  • Why does docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained address cgroups here rather than through application code?
  • How would you distinguish healthy overlay from the failure “allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent” using cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths?
  • When would this trade-off-“Hard resource caps protect hosts but can throttle legitimate bursts; overlay supports fast packaging but performs poorly for heavy in-place database writes”-justify a different Limits design?
Interview tip: Give one concrete operation-docker run -d --cpus=.5 --memory=256m --memory-swap=256m --name constrained api:2.1; docker inspect -f {{.GraphDriver.Data.MergedDir}} constrained-and one proof-cat /sys/fs/cgroup/memory.events inside the host namespace and inspect cpu.stat plus overlay mount backing paths. Then name the boundary: allocating a high Java heap while the container memory cap is only 256 MiB, or modifying lower image layers as if the top copy were persistent.

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Linux Kernel Primitives: Control Groups (cgroups) & Union File Systems with generative AI interview Q&A and hands-on practice.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Linux Kernel Primitives: Namespaces (PID, NET, MNT, UTS, IPC)
Next topic
How Docker Images Work (Read-Only Layers & Copy-on-Write)