Virtual Machine vs Container: Kernel, Isolation and Startup Time
Both let one computer run several applications isolated from each other, but they virtualize different layers. A virtual machine relies on a hypervisor that creates virtual hardware, such as CPUs, memory, disks and network devices, and each virtual machine runs a complete operating system, kernel included, on top of it. A container shares the host operating system's kernel and uses kernel features such as namespaces and cgroups to separate what processes can see and which resources they can use. Containers do not boot an operating system, so they are lighter and start faster, while virtual machines win on strength of isolation and the range of operating systems they can run. In the cloud the two are usually combined, with containers running inside virtual machines.
| Aspect | Virtual machine | Container |
|---|---|---|
| Virtualization layer | Hardware: the hypervisor creates virtual CPUs, memory, disks and network devices and hands them out | Operating system: on the host kernel, what processes can see (process IDs, network, file system and so on) and the resources they can use are separated |
| OS kernel | Each virtual machine runs its own operating system (guest OS), including its own kernel | All containers share the host kernel; container images do not include a kernel |
| Operating systems that can run | Also a different kind of OS from the host (for example, Windows on a Linux host) | Only what runs on the host kernel; Linux containers need a Linux kernel |
| Image size | Includes a whole OS, so typically measured in gigabytes | Only the application and the libraries it needs, so typically much smaller than a virtual machine |
| Startup time | Starts by booting an OS, so it often takes tens of seconds to a few minutes | Only starts processes, so it is fast, typically running within a few seconds |
| Strength of isolation | Strong: separation at the hardware level, so problems in one guest kernel rarely reach other virtual machines | Weaker than a virtual machine: the kernel is shared, so an exploited kernel vulnerability can affect the host and other containers |
| Resource efficiency | Each guest OS uses its own memory and CPU, so fewer fit on one machine | No duplicated operating systems, so more fit in the same resources |
| Typical uses | Running different kinds of OS side by side, server consolidation, IaaS virtual machines, environments that need strong isolation | Microservices, testing and delivery in CI/CD, large-scale operation with Kubernetes, keeping development and production environments the same |
Figures are typical orders of magnitude and vary by process generation and product.