4.2.41 · HinglishOperating Systems

Containers — namespaces, cgroups, difference from VMs

1,993 words9 min readRead in English

4.2.41 · Coding › Operating Systems


Container ACTUALLY kya hai?

YEH design kyun? Ek full VM poora OS duplicate karta hai sirf ek app ko isolate karne ke liye — RAM, boot time, aur disk mein waste. Zyada tar hume sirf chahiye hota hai ki app sooche woh akela hai. Linux already processes manage karta hai; toh hardware emulate karne ki jagah hum kernel ke global resources ko per process group mein partition kar dete hain.


namespaces — "main kya DEKH sakta hoon?"

Kernel kai namespace types offer karta hai, har ek alag cheez isolate karta hai:

Namespace Kya isolate karta hai (aap kya DEKHTE hain)
PID process IDs — container ka pehla process PID 1 ban jaata hai
Mount (mnt) filesystem mount table — apna root /
Network (net) interfaces, IPs, ports, routing tables
UTS hostname aur domain name
IPC shared memory / semaphores
User UID/GID mapping — andar root ≠ bahar root
Cgroup cgroup hierarchy chhupa deta hai

cgroups — "main kitna USE kar sakta hoon?"

Key controllers aur aap unme kya likhte ho:

Controller Example knob Matlab
memory memory.max = 512M group 512 MB se zyada ho toh OOM-kill
cpu cpu.max = "50000 100000" 100 ms window mein 50 ms CPU = ek core ka 50%
pids pids.max = 100 fork-bomb protection
io io.max disk bandwidth throttle karo

Containers vs Virtual Machines

Figure — Containers — namespaces, cgroups, difference from VMs
Container Virtual Machine
Kernel shared host kernel apna guest kernel
Isolation by namespaces + cgroups (software) hypervisor + virtual hardware
Boot time ms seconds
Size MBs GBs
Overhead near-native hypervisor tax
Security boundary weaker (shared kernel) stronger (hardware-enforced)
Alag OS kernel run kar sakte ho? ❌ (Linux pe sirf Linux)


Recall Feynman: ek 12-saal ke bache ko explain karo

Socho ek bada ghar hai (computer). Ek VM aise hai jaise paas mein ek bilkul alag ghar banana apni plumbing aur electricity ke saath — strong walls, lekin expensive aur build hone mein slow. Ek container aise hai jaise existing ghar ke ek room ko private bedroom bana do: tum parde lagao taaki bacha baaki ghar na dekh sake (namespaces = parde), aur tumne use ek chore-chart do jo kahe "tum sirf itna snacks aur TV time use kar sakte ho" (cgroups = limits). Yeh wahi ghar, wahi kitchen hai (ek kernel) — kaafi sasta, lekin walls sirf parde hain, toh thodi kam privacy hai.


Flashcards

Container banane wale do kernel features kya hain?
namespaces (aap kya SEE kar sakte ho usse isolate karte hain) + cgroups (aap kya USE kar sakte ho usse limit karte hain)
Namespace ___ isolate karta hai jabki cgroup ___ limit karta hai
namespace → ek global resource ka visibility/view; cgroup → resource consumption
4 namespace types batao.
PID, Mount, Network, UTS, IPC, User, Cgroup (koi bhi 4)
Child process ke liye nayi namespaces kaun sa syscall banata hai?
clone() with CLONE_NEW* flags (current ke liye unshare bhi, join karne ke liye setns)
docker exec running container mein kaise enter karta hai?
setns(fd, type) — existing container ke namespaces mein join karta hai
cpu.max = "50000 100000" ka matlab kya hai?
quota/period = 50000/100000 = 0.5 → ek CPU core ka 50%
cgroup v2 cpu.max se CPU share ka formula?
CPU share = quota / period (cores mein)
Container aur VM ke beech sabse bada ek difference?
Container host kernel share karta hai; VM apna guest kernel hypervisor ke zariye run karta hai
Linux container Windows app natively kyun nahi run kar sakta?
Container host Linux kernel ke syscalls reuse karta hai; Windows binaries ko Windows syscalls chahiye jo Linux kernel provide nahi karta
Containers VMs se faster start kyun hote hain?
Koi guest OS/kernel boot nahi karna — container bas ek process hai; sirf namespaces+cgroups set up hote hain (ms vs seconds)
Security boundary container hai ya VM, aur kyun?
VM (stronger) — hypervisor ke zariye hardware-enforced; container ek kernel share karta hai toh kernel bug sab ko affect karta hai
Container-root host-root ke barabar nahi kyun ho sakta?
User namespace — UID 0 andar ko ek unprivileged host UID se map karta hai (rootless containers)
Kya cgroups isolation dete hain?
Nahi — woh resource quantity limit karte hain; isolation (visibility) namespaces se aati hai

Connections

  • Processes and the clone/fork syscall
  • Linux Kernel — syscalls
  • Virtualization and Hypervisors (Type 1 vs Type 2)
  • Filesystems — mount and chroot
  • Networking — virtual interfaces and bridges
  • Docker images and layered filesystems (overlayfs)
  • Scheduling — CFS and CPU shares

Concept Map

isolated view by

resource limits by

shares

no second kernel unlike

duplicates

controls what you SEE

controls what you USE

types include

created via

first process becomes

docker exec uses

Container = normal process

Namespaces

cgroups

Host Kernel

Virtual Machine

Guest OS + Emulated HW

Isolation of view

Resource limits

PID, mnt, net, UTS, IPC, User

clone / unshare / setns

PID 1 inside

setns to join