Containers vs virtual machines
Two ways to box up software so that apps on one machine do not disturb each other: bring a whole operating system along, or share the one that is already there.
In 30 seconds
A virtual machine is a complete computer made of software: each one runs its own operating system. A container holds only an app and its libraries, and shares the host’s kernel, the core of the operating system.
Sharing makes containers small and quick: megabytes instead of gigabytes, about a second to start instead of a full boot.
The price is a thinner wall. Containers depend on one shared kernel to keep them apart, and they must match its operating system. VMs are heavier but more strongly separated.
Containers vs virtual machines, step by step
Sizes and times are typical, not limits: a trimmed-down “microVM” can boot in well under a second, and container images can reach gigabytes. Windows has its own containers, which need a Windows host.
- Step 1: The VM way: an OS each. Three apps have to share one server without getting in each other’s way. A virtual machine does it by giving each app a complete computer made of software: its own operating system, with its own kernel (the core that controls the hardware), running on top of a hypervisor.
- Step 2: The container way: share one. A container packs only the app and the libraries it needs, and leaves the operating system out. Every container uses the kernel of the host, and that kernel hides them from one another, so each seems to have the machine to itself. Underneath, a container is an ordinary process with a restricted view.
- Step 3: Size: what travels along. A VM is stored as a disk image holding an entire operating system, so it commonly runs to gigabytes. A container image holds the app, its libraries and a few base files, and borrows the kernel that is already running. It is commonly tens or hundreds of megabytes, so it is quick to copy and to download.
- Step 4: Start-up time. Starting a VM means booting an operating system: firmware, kernel and background services all come up before the app can. Starting a container means starting a process on a kernel that is already running. That speed is why containers suit work that is launched and thrown away on demand.
- Step 5: Isolation strength. Code that escapes from an app inside a VM still meets that VM’s own kernel and then the hypervisor, a small and carefully guarded layer. Containers all talk directly to one large shared kernel, kept apart by its namespaces (what a process can see) and cgroups (how much it may use). One kernel flaw can expose every container.
- Step 6: One kernel, one OS family. Because a container has no kernel of its own, it must be built for the kernel underneath. On a Linux host, containers based on Debian or Alpine both run, since both are Linux; a Windows program cannot. Docker on a Mac or a Windows PC quietly runs a small Linux VM to supply that kernel.
- Step 7: When to use which. Containers fit many copies of your own services that must start fast and behave the same on a laptop and a server. VMs fit when you need another operating system, your own kernel settings, or a strong wall around code you do not trust. Clouds commonly combine them: each customer’s containers run inside that customer’s VMs.
Try it yourself
Learn this first
Spotted a mistake on this page?