Containers vs VMs
EasyContainersDockerVirtualization
What's the difference between containers and virtual machines?
TL;DR
A VM virtualizes hardware: a hypervisor runs full guest operating systems, each with its own kernel. A container virtualizes the operating system: containers are isolated processes that share the host's kernel, using Linux namespaces (what a process can see: PIDs, network, mounts, users) and cgroups (what it can use: CPU, memory, I/O). Containers start in milliseconds, weigh megabytes and pack densely; VMs give stronger isolation and can run a different OS kernel.
Key points
| Container | VM | |
|---|---|---|
| Isolation boundary | Shared kernel (namespaces, cgroups) | Hypervisor, separate kernel |
| Startup | Milliseconds | Seconds to minutes |
| Size | MBs | GBs |
| Security | Weaker: a kernel exploit can escape | Stronger |
| OS | Same kernel as the host (Linux on Linux) | Any guest OS |
- An image is a read-only, layered filesystem plus metadata. A container is a running instance with a thin writable layer on top.
- Docker on macOS and Windows runs containers inside a small Linux VM.
- They combine: in the cloud, containers usually run on VMs.
- For stronger isolation of untrusted workloads: gVisor, Kata Containers or Firecracker microVMs (used by AWS Lambda and Fargate).
- Container hardening: run as non-root, drop Linux capabilities, read-only root filesystem, seccomp profiles.
Example
bash
docker run --rm --memory=256m --cpus=0.5 --read-only --user 1000 nginx:alpine
# --memory and --cpus → cgroups; separate PID and network → namespacesCommon follow-ups
- Why are containers immutable? You rebuild and redeploy the image instead of patching running containers, so every environment runs exactly the same artifact.
- Where does container data go? The writable layer is lost when the container is removed. Persist data in volumes or external storage.
What interviewers look for
Tip
"Shared kernel" vs "separate kernel", namespaces and cgroups by name, and the security trade-off.
- Red flag: "containers are lightweight VMs."