Galle, LK--:--5/6 sites live

Docker Deep Dive 1: Containers Under the Hood (Namespaces & Cgroups)

පිටුව 13: Docker රහස් 1: Container එකක් කියන්නේ VM එකක් නෙවෙයි! Linux Namespaces සහ Cgroups

Container එකක් ඇතුළේ ඇත්තටම වෙන්නේ මොකක්ද? Linux Kernel Namespaces (PID, NET, MNT, UTS), Cgroups (CPU & RAM සීමා කිරීම), සහ Containers vs VMs සන්සන්දනය.

14 Sept 20264 min readSelf Guide · Stage 11#Docker#Namespaces#SRE Labs

Lab 13 of 20 · Docker Internals · 5 hands-on activities · bilingual (English + සිංහල)

පිටුව 13: Docker රහස් 1: Container එකක් කියන්නේ VM එකක් නෙවෙයි! Linux Namespaces සහ Cgroups

Container එකක් ඇතුළේ ඇත්තටම වෙන්නේ මොකක්ද? Linux Kernel Namespaces (PID, NET, MNT, UTS), Cgroups (CPU & RAM සීමා කිරීම), සහ Containers vs VMs සන්සන්දනය.

💡 සරල සිංහලෙන්: Container එකක් කියන්නේ මොකක්ද?

VM එකක් සහ Container එකක් අතර වෙනස (හෝටල් කාමර සහ වෙනම ගෙවල්):

  • Virtual Machine (වෙනම ගෙයක්): ඔබ වෙනම ඉඩමක් අරන්, අත්තිවාරම් දාලා, වෙනම වතුර මෝටර්, වෙනම විදුලි මීටර් දාලා හදන වෙනම ගෙයක් වගේ. ආරක්ෂාව වැඩියි, හැබැයි හදන්න සල්ලි ගොඩක් යනව, හදන්න මාස ගණන් යනවා (GB ගණන් ලොකුයි, Boot වෙන්න විනාඩි ගාණක් යනවා).
  • Container (හෝටල් කාමරයක්): එකම ලොකු ගොඩනැගිල්ලක් (Host Linux Kernel) ඇතුළේ වෙනම කාමර බෙදලා තියෙනවා වගේ. කාමරේ ඇතුළේ ඉන්න කෙනාට පේන්නේ එයා තනිවම ඉන්නවා වගේ (Namespaces). හැබැයි වතුරයි කරන්ට් එකයි ඔක්කොම එන්නේ එකම ප්‍රධාන ලයින් එකෙන්. ක්ෂණිකව තත්පරයෙන් කාමරේට යන්න පුළුවන් (Starts in milliseconds!).
  • Cgroups (මීටරය): හෝටල් කාමරේ ඉන්න කෙනාට කරන්ට් එකෙන් උපරිම 500W ගන්න පුළුවන් කියලා දාන සීමාවයි Cgroups කියන්නේ. එතකොට කාටවත් මුළු හෝටලේම කරන්ට් කපන්න බෑ!

Architectural Foundation & Systems Diagram

A container is NOT a mini Virtual Machine. A container is simply a standard Linux process that has been isolated using two fundamental Linux kernel features: Namespaces and Control Groups (cgroups).

  • 🔮 Linux Namespaces (Isolation) — Creates the illusion that the container is the only OS running:
    • PID: Container thinks its process is PID 1!
    • NET: Dedicated virtual network interfaces & IP.
    • MNT: Isolated filesystem mount points.
    • UTS: Isolated hostname.

  • 🛑 Control Groups (cgroups) — Resource metering and enforcement. Prevents any single container from hogging 100% of host CPU cores, exhausting RAM, or bringing down the entire server.

  • ⚙️ Docker Engine & containerd — The Docker CLI talks via REST API to dockerd, which invokes the containerd and runc runtime to instruct the Linux kernel to create the namespaces and cgroups.

Step-by-step practical lab

Step 01: Inspect Linux Kernel Namespaces Isolating Processes

සිංහලෙන් — Container එකක් හුදකලා (Isolate) කර තබන Linux Kernel Namespaces පරීක්ෂා කරමු.

Inspect the active namespace file descriptors (ipc, mnt, net, pid, user, uts) that give containers their isolation illusion.

ExampleShell
ls -l /proc/$$/ns
  • /proc/$$/ns : වත්මන් Shell process එකට අයත් Linux Kernel Namespaces ලැයිස්තුව පෙන්වයි.

Expected output

Output
lrwxrwxrwx 1 sre-engineer devops 0 Sep 16 13:00 ipc -> 'ipc:[4026531839]'
lrwxrwxrwx 1 sre-engineer devops 0 Sep 16 13:00 mnt -> 'mnt:[4026531840]'
lrwxrwxrwx 1 sre-engineer devops 0 Sep 16 13:00 net -> 'net:[4026531992]'
lrwxrwxrwx 1 sre-engineer devops 0 Sep 16 13:00 pid -> 'pid:[4026531836]'
lrwxrwxrwx 1 sre-engineer devops 0 Sep 16 13:00 uts -> 'uts:[4026531838]'

SRE Tip: Containers are not VMs! They share the host kernel. Linux Namespaces isolate what a process can SEE (PIDs, network, mounts).

Step 02: Verify Docker Client & Server Engine API Version

සිංහලෙන් — Docker Client සහ Docker Server Engine අනුවාද පරීක්ෂා කරමු.

Query Docker Client and Engine API versions, Go runtime compilation version, and Git commit hash.

ExampleShell
docker version
  • version : Client CLI එක සහ background dockerd daemon එක අතර API compatibility පෙන්වයි.

Expected output

Output
Client: Docker Engine - Community
 Version:           26.1.4
 API version:       1.45

Server: Docker Engine - Community
 Engine:
  Version:          26.1.4
  Storage Driver:   overlay2
  Cgroup Driver:    systemd
  Cgroup Version:   2

SRE Tip: Docker communicates internally via a Unix socket (/var/run/docker.sock) using a standard HTTP REST API.

Step 03: Audit System-Wide Docker Telemetry & CGroup Driver

සිංහලෙන් — Docker Storage Driver (overlay2) සහ Cgroup Version එක බලාගනිමු.

Inspect system-wide Docker engine telemetry: storage driver (overlay2), cgroup driver (systemd), container count, and CPUs.

ExampleShell
docker info
  • info : Docker එන්ජිමේ සම්පූර්ණ පද්ධතිමය දත්ත සටහන ලබා දෙයි.

Expected output

Output
Containers: 2
 Running: 2
 Images: 4
Server Version: 26.1.4
Storage Driver: overlay2
Cgroup Driver: systemd
Cgroup Version: 2
Kernel Version: 6.8.0-45-generic
CPUs: 4
Total Memory: 7.685GiB

SRE Tip: Cgroup v2 provides unified resource hierarchy, allowing strict memory.max and cpu.weight enforcement across containers.

Hands-on practice activities

Work through these yourself — tick them off as you go.

  • Activity 61: Inspect Process Linux Kernel Namespaces (Drill) — Inspect the active namespace file descriptors (ipc, mnt, net, pid, user, uts) that isolate processes.
  • Activity 62: Query Docker Engine Version and API (Drill) — Verify Docker Client, Docker Server Engine, API version, and Git commit hash.
  • Activity 63: Inspect System-Wide Docker Telemetry (Drill) — Display total containers, images, storage driver (overlay2), and CGroup version (v2).
  • Activity 64: Understand Container Kernel Sharing (Checkpoint) — A container CANNOT run a Windows kernel on a Linux host, because containers share the underlying Linux kernel!
  • Activity 65: Linux Namespaces Function Quiz (Quiz) — Which Linux kernel feature provides process, network, and mount isolation for containers? Answer: Linux Namespaces.

Keep going: ← Lab 12 · Lab 14 → · Track your progress in the Self Guide

ExerciseKnowledge check

Which Linux kernel feature provides process, network, and mount isolation for Docker containers? / Containers සඳහා isolation ලබාදෙන Linux kernel පහසුකම කුමක්ද?

Finished this one?

Part of Self Guide · Stage 11: SRE Labs II — cloud, containers and delivery. Progress is saved in this browser.

Guide