Sub topic 10
Containers and VMs Side by Side
This chapter assumes you know what a process and a kernel are, and that you have seen the earlier chapters on hypervisors, namespaces, cgroups, and image layers. Here we put the two models next to each other and compare them on four points: isolation, overhead, startup time, and portability.
The core difference
A virtual machine (VM) virtualizes hardware. A hypervisor presents virtual CPUs, memory, disks, and network cards, and a full guest operating system boots on top of them. Each guest has its own kernel.
A container virtualizes the operating system's view of itself. Namespaces limit what a process can see, and cgroups limit what it can use. Every container on a host shares that host's kernel.
The boundary in the VM stack is the hypervisor. The boundary in the container stack is the kernel itself.
Comparison table
| Property | Container | Virtual machine |
|---|---|---|
| Kernel | Shared with the host | One per guest |
| Isolation boundary | Kernel features (namespaces, cgroups) | Hypervisor and hardware virtualization |
| Typical size | Megabytes to a few hundred megabytes | Gigabytes (includes a full OS) |
| Startup time | Milliseconds to about a second | Seconds to minutes |
| Memory overhead | Close to none beyond the app | A guest kernel and OS services per VM |
| Guest OS choice | Must match the host kernel type | Any OS the hypervisor supports |
| Density per host | Often dozens to hundreds | Often a handful to dozens |
These are typical ranges, not guarantees. A minimal unikernel or a tuned microVM can start in well under a second, and a bloated container image can take a long time to pull.
Isolation
A VM guest cannot see the host's processes or files, because it runs a separate kernel on virtual hardware. To affect the host, an attacker must escape through the hypervisor, which exposes a small and heavily reviewed interface.
A container process is an ordinary host process. The kernel enforces its limits, and every system call goes to the same kernel that serves the other containers. A kernel bug can therefore cross the boundary. The next chapters cover this in more detail.
Overhead and startup
A VM must boot a kernel and start system services. It also reserves memory for its guest OS, even when the app inside is small. A container starts one process in a prepared filesystem, so there is no boot sequence.
You can see the shared kernel directly on a Linux host:
uname -r
docker run --rm alpine uname -rBoth commands print the same kernel release. The Alpine container has no kernel of its own.
You can also measure startup time:
time docker run --rm alpine trueOn a machine where the image is already pulled, this usually finishes in well under a second. Booting a typical VM to a login prompt takes much longer.
Portability
A container image bundles an app with its libraries and userland files, so the same image runs on any host with a compatible kernel and CPU architecture. Two limits apply:
- An image built for
linux/arm64does not run natively on anamd64host. Emulation works but is slow. - A Linux image needs a Linux kernel. Docker Desktop on macOS and Windows runs one inside a hidden VM.
A VM image carries a whole OS, so it is larger. It can run any guest OS the hypervisor supports, including Windows on a Linux host. Moving VM disk images between hypervisors may require format conversion.
Common mistakes
- Assuming a container is a small VM. Containers have no boot process, and
pson the host shows their processes. - Assuming the base image sets the kernel.
FROM ubuntugives you Ubuntu's userland. The kernel is still the host's. - Assuming faster means safer or less safe. Speed and isolation strength are separate properties. Judge security by the boundary, not by startup time.
Exercise
A team must run three copies of a stateless web service on one Linux host, and each copy must start within a second when load rises. A fourth workload needs a custom kernel module. Which workloads suit containers, and which suit a VM?
Show answer
The three web service copies suit containers. They share the host kernel, start in well under a second, and add almost no memory overhead. The workload that needs a custom kernel module needs a VM, because a container cannot load its own kernel code and can only use the host kernel.
Exercise
You run uname -r on a Linux host and again inside an Alpine container on that host. The two outputs are the same. What does this show, and would you expect the same result inside a VM guest?
Show answer
It shows that the container uses the host kernel and has no kernel of its own. A VM guest runs its own kernel, so its release could differ from the host's.