noted.do
The Tech Guy

Virtual Machines vs Containers

People mix these up constantly, and it doesn't help that they're often described with the same vague line about "running things in isolation." They both do that. They just draw the line in very different places.

A virtual machine pretends to be a whole computer. A piece of software called a hypervisor (VMware, Hyper-V, KVM, VirtualBox, Proxmox and so on) carves up the real hardware and presents each VM with its own virtual CPU, memory, disk and network card. Inside, you install a full operating system with its own kernel, exactly like you would on a physical machine. The VM has no idea it's sharing anything. You can run Windows next to Linux next to BSD on the same box.

A container doesn't pretend to be a computer. It's a regular process running on the host's operating system, with features of the Linux kernel used to fence it off. Namespaces give it its own view of the filesystem, network and process list, so it thinks it's alone. Cgroups limit how much CPU and memory it can use. Every container on a host shares that host's kernel. There's no second operating system booting up, just your app plus the libraries and files it needs, packaged into an image.

That one difference explains almost everything else.

Containers are light. They start in about the time it takes the app itself to start, and you can run a lot of them on modest hardware, because you're not paying for a full OS each time. Images are small, easy to share and easy to rebuild, which is why Docker took off for deploying apps.

VMs are heavier but more separate. Each one boots a whole OS, uses more memory and disk, and takes longer to start. In exchange, the isolation is stronger. The boundary between a VM and its host is the hypervisor, which is a much smaller and more carefully guarded surface than a shared kernel. If you're running code you don't trust, or keeping different customers apart, that matters.

Because containers share the kernel, they also share its limits. A Linux container needs a Linux kernel. That's why Docker Desktop on a Mac or Windows machine quietly runs a small Linux VM behind the scenes; your containers live inside it. Windows has its own containers for Windows apps, but you can't run a Windows container directly on a Linux host.

Security-wise, containers are reasonably safe when set up sensibly, but it's worth not getting casual. Don't run container processes as root if you can avoid it, don't run things with --privileged unless you really understand why, and be careful about mounting the Docker socket into a container, since that effectively gives it control of the host.

In practice, people often use both. A common home lab setup is a hypervisor like Proxmox running a few VMs, with one of those VMs running Docker and a pile of containers. Cloud providers do the same thing at a huge scale: your "server" is a VM, and you run containers on top of it. You get the hard separation of the VM where it counts and the convenience of containers for the apps themselves.

So how do I pick? If I'm deploying an app or a service, I reach for a container. If I need a different operating system, a full desktop, a different kernel, or stronger isolation for something I don't trust, I use a VM. And if I'm not sure, a VM with containers inside is rarely the wrong answer.

Comments

Be kind. See the rules.