VMs vs containers
A VM emulates a whole machine and boots its own OS; containers share the host's kernel. See the layers from the hardware up, race them to start, then watch how far it spreads when something breaks.
What you'll take away
- Tell a VM from a container: a whole OS of its own, or a process on a shared kernel
- Explain why a container starts in about a second and a VM takes tens of seconds
- Tell namespaces from cgroups, and set
--memoryso one box can't starve the rest - Know why clouds run containers inside VMs: a kernel hole stops at the VM's wall
The 1-minute version
Tap a point and the picture below plays that step.
The clock in the picture runs ten times fast, and every number is the model's rough figure, not a measurement
Two containers on one machine: one from an Alpine image, one from Debian. You run uname -r in each. What do you get?
Three of the same server stand in one machine hall: bare metal, virtual machines and containers, each drawn as layers from the hardware up to the apps. Each step is one thing happening on one of them, and the band at the bottom of the picture says what happened and why. Every number is the model's rough figure (the start clock runs faster than real time), not a measurement.
Compare them all: VMs vs containers
The same three apps in every tile, all started at once (the clock runs ten times fast). Then app A starts eating memory, then a hole turns up in a kernel. See which comes up first, what it costs in memory, and how far each problem spreads.
Use it when You need every last bit of the machine: a big database, or work that drives the hardware itself
Skip it when Many apps or teams on one machine: versions clash, and one app eating memory drags the rest down
Rough model numbers: a VM boots an OS in tens of seconds, a container starts in about a second, a real machine takes minutes to reboot. Not measurements. The containers have --memory set. Press any tile to see it large above, with when it fits.
The three, side by side
| Bare metal | VM | Container | |
|---|---|---|---|
| Kernel | The machine's one | One each | The host's, shared |
| Start a new one | A reboot takes minutes | Boot an OS: tens of seconds | About a second |
| Size | The whole machine | An image of several GB | An image of MBs to hundreds of MB |
| Wall | None: apps share one OS | Thick: the hypervisor | Thinner: namespaces and cgroups |
A container isn't a small VM
A container is an ordinary process the kernel puts in a box: it sees its own files, processes and network (namespaces) and can use only so much CPU and memory (cgroups). There's no OS to boot, so it starts fast, but it also means every container leans on the same kernel.
So why do clouds still use VMs?
Because a VM's wall is thicker, so one machine can run many customers' work with less worry. Most clouds rent you VMs, and you run containers inside them. Some use microVMs, which start almost as fast as a container but still have their own kernel.
In real code
# An image: the app and the few files it needs. No kernel, no OS to boot: the host's kernel runs it.
FROM alpine:3.20
RUN adduser -D app
WORKDIR /app
COPY hello.sh .
USER app
CMD ["sh", "hello.sh"]
docker build, then docker run (checked in Docker, nothing pulled)It printed: pid 1 · user app · Alpine Linux v3.20A small image on Alpine: the app's files and what it needs, no kernel. Checked with a real docker build (nothing pulled), then run once; below is what the container printed. It's the first process (pid 1) of its own little world.
Common questions
What's the difference between a VM and a container?
A VM emulates a whole machine and boots its own OS. A container is a process sharing the host's kernel. So containers are much lighter and quicker to start, and VMs get the thicker wall.
Why does a container start so much faster than a VM?
A VM boots a whole OS, from pretend firmware through a kernel to its services, which takes tens of seconds. A container has nothing to boot: its namespaces and cgroups are set up and one process starts on a kernel that's already running, in about a second.
Does a container have its own kernel?
No. An image holds only userland files, like libraries, a shell and the app, never a kernel. Run uname -r in an alpine and a busybox container and you get the same answer: the host's kernel.
How do namespaces and cgroups differ?
Namespaces decide what a container can see: its own files, processes, network and hostname. Cgroups decide how much it can use: CPU, memory, I/O. Both are Linux kernel features.
One container eats all the memory. Do the others suffer?
If you set --memory, the kernel holds it to that limit (that's a cgroup) and the others are fine. Docker sets no limit by default, though, and without one it can starve the others just like an app on bare metal.
How does Docker on a Mac run Linux containers?
It quietly runs a small Linux VM, and every container uses that VM's kernel. A Linux container really does need a Linux kernel.
If containers are lighter, why do clouds still use VMs?
A VM's wall is thicker, so one machine can run many customers' work with less worry. Most clouds rent you VMs and you run containers inside them. Some use microVMs, which start almost as fast as a container and still have their own kernel.
What does a hypervisor do?
It splits a real machine into pretend hardware for each VM (CPU, memory, disk, network) and keeps VMs from seeing each other. Type 1 runs on the machine itself (KVM, ESXi); type 2 runs on top of an OS (VirtualBox).
Next lesson
How Kubernetes looks after containersLevel: Deep
Containers start in a second; next, Kubernetes, which tells them where to run, replaces the ones that die and adds more.