Course contents · Lesson 1 of 22

Virtual Machines vs Containers: What's the Difference?

Containers 101 · Lesson 1 of 22 · 8 min Foundations

Your team lead says the new app will "run in containers", while a friend's company runs everything "on VMs". Both share one computer between programs, so what is the difference? This lesson settles virtual machines vs containers: how each splits up a computer, why containers start faster and weigh less, and when to choose which.

Virtual machines: a whole computer made of software

Every computer runs an operating system (OS), such as Windows, macOS or Linux. Its core is the kernel: the program that controls the processor, memory, disks and network on behalf of every other program.

A virtual machine (VM) is a computer made of software. A program called a hypervisor, such as KVM (Kernel-based Virtual Machine), Microsoft Hyper-V or VirtualBox, splits one physical machine into several VMs and gives each its own virtual processors, memory, disk and network card.

Each VM boots a full guest operating system with its own kernel, so one VM can run Windows while its neighbour runs Linux. With built-in processor support, most of the guest's work runs directly on the real processor, not through slow imitation.

The cost: three VMs means three operating systems to store, boot, update and hold in memory.

Containers: walled-off processes sharing one kernel

A process is a running program, such as your browser. A container is one or a few ordinary processes that the host's kernel walls off from the rest of the machine. There is no guest OS inside: all containers on a machine share the one kernel already running.

On Linux, two kernel features build the walls. Namespaces control what a process can see: its own list of processes, network addresses and files. Inside a typical container, the main process sees itself as process number 1 and cannot see the host's other programs.

Control groups (cgroups) control how much a process can use: a cap on memory, a share of processor time, a limit on how many processes it may start. In the analogy, namespaces are the room's door and cgroups its fair share of hot water.

A container still brings its own files: the app plus its libraries (ready-made code it relies on) and settings, packed into an image, covered in lesson 2. A container runtime, such as Docker, is the software that asks the kernel to build these walls. The diagram compares the two stacks; notice what each copy carries.

Two stacks running three apps on one physical server. Virtual machines: hardware, then a hypervisor, then three VMs, each with its own guest OS and kernel, libraries and app. Containers: hardware, then a host OS with one shared kernel, then a container runtime such as Docker, then three containers holding only libraries and an app, with no guest OS inside
Each VM carries a whole guest OS; containers carry only app and libraries, sharing one kernel.

Side by side: speed, size, isolation and density

Four practical differences follow from that picture. The figures are typical, rough orders of magnitude, not promises.

Startup: a VM must boot a whole OS before your app runs, which takes tens of seconds to minutes. A container starts as a process on an already running kernel: once its image is on the machine, it is often ready in well under a second, plus your app's own start-up time.

Size: a VM's disk holds a complete OS, usually several gigabytes. A container image holds only what the app needs; the official image of Alpine, a tiny version of Linux, is a few megabytes.

Isolation: an attack inside a VM must break through the hypervisor to reach its neighbours. Containers share a kernel, so one kernel flaw can let a process escape its walls and reach other containers or the host: the walls between rooms are thinner.

Density: a VM is given its memory up front, often a gigabyte or more, and its own OS uses part of it. A container uses roughly what its app uses, so one server fits many more containers than VMs, as the chart shows to scale.

Bar chart of illustrative typical values drawn to scale. Time to start: a VM about 30 seconds, a long bar, because it boots a whole OS; a container under 1 second, a thin sliver, because it starts a process. Memory per copy of a small app: a VM about 1 GB for the guest OS plus the app; a container tens of MB, just the app
Drawn to scale, the container bars are slivers: no OS to boot, and none to hold in memory.

The hidden Linux VM on your Mac or Windows laptop

A common misunderstanding is that containers run directly on any laptop. Most are Linux containers, which need a Linux kernel, and macOS and Windows have different kernels.

Docker Desktop (an app for running containers on a laptop) and similar tools quietly start a small Linux VM and run your containers inside it. On Windows that is usually WSL 2 (Windows Subsystem for Linux), which runs a real Linux kernel in a lightweight VM. Docker Desktop uses a VM even on Linux; Docker Engine installed directly on Linux does not.

So on a laptop both ideas work together: a VM supplies the Linux kernel, and the containers share it. Windows also has Windows containers, which share the Windows kernel, but this course uses Linux containers.

When to use which, and why it is often both

The flowchart boils the choice down to one question.

Flowchart. Start: I need to run an app. Decision: do I need a different OS kernel or the strongest isolation? Yes leads to a virtual machine, with its own guest OS and kernel, for example a Windows app on a Linux server. No leads to a container, which shares the host's kernel, for example many copies of a web app. A note says it is often both: containers running inside a VM
Only a different kernel or the strongest walls call for a VM; for everything else, a container is lighter.

Choose a VM to run a different OS, such as a Windows-only program on a Linux server, or to keep untrusted code, such as programs uploaded by strangers, behind the strongest walls.

Choose a container to package an app so it runs the same everywhere, and to run many copies on one machine. Copies start and stop in moments, which Kubernetes, later in this course, relies on.

Often you use both. A rented cloud server is usually itself a VM, with containers inside: the VM separates customers, the containers pack apps densely. Cloud Compute Explained shows the cloud options.

Check yourself

A flaw is found in the Linux kernel. On a server running 50 containers, how many kernels need patching?

One, the host's: all 50 containers share it, so until it is patched the flaw puts every one of them at risk.

A colleague wants to run a Windows-only accounting program on the team's Linux server. Container or VM?

A VM: the program needs a Windows kernel, and a container can only use its host's kernel, here Linux.

On your Mac you start a Linux container, but its process never appears in ps aux. Is it really running?

Yes, inside the Linux VM that Docker Desktop starts: the process belongs to that VM's kernel, so the Mac's process list cannot see it.