Skip to content
Tutorials Center
My learning

Topics

Lesson 60 of 66 ยท Protection, Security and Virtualization

Containers, Namespaces and cgroups

Understand how containers isolate processes with Linux namespaces, limit resources with cgroups, and share image layers, how they differ from virtual machines, with Python simulations.

A virtual machine gives each workload its own complete operating system. That is strong isolation, but heavy: each VM boots a kernel, reserves gigabytes of memory and takes seconds to minutes to start. If you only want to keep one application and its dependencies separate from the others, there is a lighter way: ask the one kernel to show each application a private, restricted view of the system. That is a container.

A container is not a special kernel object. It is an ordinary process (or group of processes) on the host whose view and resources have been limited by three kernel features: namespaces (what it can see), cgroups (how much it can use) and a layered root file system (what files it has). Docker, Podman and Kubernetes build on these.

Containers versus VMs

Virtual machine Container
Isolation unit Whole machine with its own kernel Process group sharing the host kernel
Startup Seconds to minutes Milliseconds to seconds
Memory overhead Hundreds of MB to GB Close to none
Density Tens per host Hundreds or thousands
Isolation strength Strong (hypervisor boundary) Weaker (kernel is shared)
Guest OS Any (Windows on Linux, etc.) Same kernel family as the host
flowchart TB
    subgraph VM["Virtual machines"]
      direction TB
      A1["App A + libs"] --> GO1["Guest OS A"]
      A2["App B + libs"] --> GO2["Guest OS B"]
      GO1 --> HV["Hypervisor"]
      GO2 --> HV
    end
    subgraph C["Containers"]
      direction TB
      B1["App A + libs"] --> RT["Container runtime"]
      B2["App B + libs"] --> RT
      RT --> K["Shared host kernel"]
    end

Containers skip the guest OS, which is why they are small and fast, and also why a kernel bug affects all of them.

Namespaces: what a process can see

A namespace wraps a global system resource so processes inside see their own private copy. Linux has several kinds:

Namespace Isolates Effect inside the container
pid Process IDs The first process is PID 1; it cannot see host processes
net Network devices, IPs, ports Its own network stack, own port 80
mnt Mount points Its own root file system
uts Hostname Its own hostname
ipc Shared memory, message queues No access to other containers' IPC
user User and group IDs Root inside is an unprivileged user outside
cgroup The view of cgroups Own cgroup root
time Clocks (offsets) Own boot/monotonic time offsets

Because every namespace is separate, a container can run as PID 1, think its hostname is web-1, listen on port 80 and see only its own files, while on the host it is just process 48213 listening on port 32768.

Python A PID namespace: two views of the same processes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Inside container web, nginx is PID 1; on the host the same process has PID 102.

cgroups: how much a process can use

Control groups (cgroups) put processes into a hierarchy and limit or account for the resources of each group. Controllers include:

  • memory: a hard limit (exceeding it triggers the cgroup's OOM killer),
  • cpu: shares or a quota, such as "at most 50% of one core",
  • pids: a maximum number of processes (stops fork bombs),
  • io: disk bandwidth,
  • devices: which devices can be opened.

Without cgroups one greedy container could take all memory and starve everyone else. With them, the kernel enforces fair sharing, which is how cloud platforms offer "0.5 vCPU, 256 MB" plans.

Python cgroup limits: CPU quota and a memory OOM kill

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The third allocation breaks the limit. Only that container's process is killed, not the whole machine. The fork bomb stops at three processes.

Layered file systems

A container's files come from an image: a stack of read-only layers, each one a set of added, changed or deleted files. Layers are shared between images, so ten images based on the same Linux base store it once. At run time a thin writable layer is placed on top using a union file system (OverlayFS). A read looks through the layers from the top down and returns the first match; a write to an existing file copies it up into the writable layer first (copy-on-write); deleting a file hides it with a marker. When the container is removed its writable layer is discarded, which is why important data goes in volumes.

Python Union layers with copy-on-write

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Because the base and application layers are never modified, any number of containers can share them safely.

Security of containers

The shared kernel is the weak spot: a kernel exploit can escape every container on the host. Common hardening steps follow least privilege:

  • run as a non-root user (or use the user namespace),
  • drop Linux capabilities the app does not need,
  • filter system calls with seccomp, and confine with AppArmor or SELinux,
  • use a read-only root file system,
  • keep images small and patched, and scan them,
  • for hostile or multi-tenant code, add a VM boundary (gVisor, Kata Containers, Firecracker).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What do Linux namespaces provide to a container?

    Namespaces control what a process can see. cgroups control how much it can use.
  2. 2. Which feature stops a container from using all of the host's memory?

    A cgroup memory limit is enforced by the kernel, and exceeding it triggers the OOM killer within that cgroup.
  3. 3. Why do containers start faster than virtual machines?

    Starting a container is mostly creating a process with namespaces and cgroups, not booting a kernel.
  4. 4. What happens when a container modifies a file that lives in a read-only image layer?

    Copy-on-write keeps shared layers intact so many containers can use the same image.
  5. 5. What is the main security drawback of containers compared with VMs?

    The isolation boundary is the kernel interface, which is much larger than the narrow hypervisor interface.

Summary

  • Containers are isolated processes on a shared kernel, built from namespaces (visibility), cgroups (resource limits) and layered images (files).
  • They are lighter and faster than VMs but provide weaker isolation, so apply least privilege, seccomp and, when needed, a VM boundary.
  • Image layers are read-only and shared; a writable top layer uses copy-on-write and disappears with the container unless data lives in a volume.

Next: Project: Build a CPU Scheduler Simulator