Virtualization & Containers
Hypervisors (type 1 and 2), hardware-assisted virtualization, VMs vs containers, Linux namespaces and cgroups, and how Docker builds on them.
Cloud computing runs on two technologies: virtual machines, which virtualise hardware so many operating systems share one physical server, and containers, which virtualise the operating system so many isolated applications share one kernel.
Each VM runs a full guest OS kernel on virtual hardware provided by a hypervisor.
- Strong isolation (separate kernels)
- Can run different OSes (Windows on a Linux host)
- Heavier: GBs of memory, boot in seconds to minutes
Each container is a group of processes on the host kernel, isolated by namespaces and limited by cgroups.
- Lightweight: MBs, start in milliseconds
- Same kernel as the host (Linux containers need a Linux kernel)
- Weaker isolation: a kernel exploit can escape
Hypervisors
| Type | Runs on | Examples |
|---|---|---|
| Type 1 (bare metal) | Directly on the hardware | KVM (part of Linux), Xen, VMware ESXi, Hyper-V |
| Type 2 (hosted) | As an application on a host OS | VirtualBox, VMware Workstation, Parallels |
How guests run safely
A guest kernel expects to run privileged instructions, but it mustn't control the real hardware. Approaches over time:
- Trap and emulate — run the guest unprivileged; privileged instructions trap into the hypervisor, which emulates them. Classic x86 had instructions that didn't trap, so this didn't work directly.
- Binary translation — rewrite problematic guest instructions on the fly (early VMware).
- Paravirtualisation — modify the guest to call the hypervisor explicitly (early Xen). Today used for fast I/O drivers (virtio).
- Hardware-assisted virtualisation — Intel VT-x / AMD-V add a new privilege mode for the hypervisor; EPT/NPT (nested page tables) let the MMU translate guest-virtual → guest-physical → host-physical in hardware. This is how all modern VMs run, at close to native speed.
MicroVMs like AWS Firecracker (used by Lambda and Fargate) strip the virtual hardware down to the minimum and boot in about 125 ms, combining VM-level isolation with near-container speed.
Containers: namespaces + cgroups
A container isn't a kernel feature by itself. It's a normal process started with a combination of features:
Namespaces — what a process can see
| Namespace | Isolates |
|---|---|
pid | Process IDs: the container sees its own PID 1 |
net | Network interfaces, IPs, ports, routing tables |
mnt | Mount points: its own root filesystem |
uts | Hostname |
ipc | System V IPC, POSIX message queues |
user | User IDs: root inside can map to an unprivileged user outside |
cgroup | Its view of the cgroup hierarchy |
cgroups — what a process can use
Control groups limit and account for resources: CPU (quota and shares), memory (limit → OOM kill), block I/O, and number of PIDs. docker run --memory=512m --cpus=1.5 and Kubernetes resource limits are cgroups.
Plus: a layered filesystem and security filters
- Image layers stacked with OverlayFS: read-only layers shared between containers plus a thin writable layer on top. That's why pulling and starting images is fast.
- seccomp filters which system calls are allowed, capabilities split root's powers, and AppArmor/SELinux add mandatory access control.
# A "container" by hand, using only Linux tools:
sudo unshare --pid --fork --mount-proc --uts --net bash
hostname sandbox # only changes the hostname inside the new UTS namespace
ps aux # only shows processes in this PID namespaceDocker in one picture
Container images and runtimes are standardised by the OCI specifications, so images built with Docker run on containerd, CRI-O, Podman and Kubernetes. More: Docker, Kubernetes.
Stronger isolation for containers
When you run untrusted code, shared-kernel containers may not be enough:
- gVisor — a user-space kernel that intercepts the container's system calls.
- Kata Containers / Firecracker — each container (or pod) inside a lightweight VM.
- WebAssembly runtimes — sandboxed at the language level.
- VMs virtualise hardware (separate kernels); containers virtualise the OS (shared kernel).
- Type 1 hypervisors run on bare metal; hardware support (VT-x, EPT) makes VMs fast.
- A container = a process + namespaces (what it sees) + cgroups (what it uses) + a layered filesystem.
- Docker/OCI images are layered and shared; runtimes like runc create the namespaces.
- For untrusted workloads, use microVMs or sandboxed kernels for stronger isolation.
QWhat is the difference between a container and a VM?easy
A VM runs its own kernel on virtualised hardware provided by a hypervisor, giving strong isolation at the cost of size and boot time. A container is a set of host processes isolated with namespaces and limited by cgroups; it shares the host kernel, so it's lighter and faster to start, but isolation is weaker.
QCan you run a Windows container on a Linux host?medium
Not natively: containers share the host kernel, and Windows binaries need the Windows kernel. You need a VM (Docker Desktop on macOS/Windows runs a Linux VM for exactly this reason).