Docker Containers vs Virtual Machines: What's the Difference?
Docker containers vs virtual machines for beginners, measured on one real Linux server: what each one really is (a VM boots its own operating system and kernel on fake hardware from a hypervisor, KVM here; a container is a fenced-off process that shares the server's kernel), the kernel test (an Ubuntu 20.04 container reports the server's kernel, an Alpine VM its own), why a Windows image won't run on a Linux server, start-up time (nginx ready in under half a second vs about ten), what the server sees (container processes vs one qemu process), memory and disk, namespaces (what a container can see), the isolation trade-off (system calls vs fake hardware), Docker Desktop's Linux VM on a Mac, and when to choose each.

Same nginx, two ways to run it
Your app needs a server: a virtual machine, or a Docker container? People say a container is just a lighter VM. It isn't.

One server runs nginx, a web server, two ways. In a container, it's serving pages in 0.46 seconds. In a virtual machine, 9.9 seconds.

22 times longer. Not because a container is a smaller computer: it isn't a computer at all. And that tells you where your app belongs.

What each one really is
Picture two ways to live. A virtual machine is a house: its own foundation, its own walls, its own plumbing. A container is a flat in a building: your own front door and your own rooms, but one foundation and one set of pipes, shared with every other flat.
On the server, the house is a virtual machine. Software called a hypervisor fakes a whole computer for it: processors, memory, a disk. On this server, that hypervisor is KVM. The VM boots its own operating system on that, with its own kernel. The kernel is the core of an operating system: the part that talks to the hardware and runs every program. A container boots nothing. It's just a normal running program on the server. The server's own kernel fences it off, and every container shares that same kernel.
On this server, two tools sit on one kernel. Docker starts containers, and a command called virsh starts virtual machines, using KVM, which is built into the Linux kernel itself. So the server's kernel is in charge of both: containers run directly on it, while each VM it starts boots a kernel of its own.
Start-up: a boot, or a process
Back to the race from the start. This command starts nginx in a container, in the background. It also connects port 8080 on the server to nginx inside. A port is just a numbered door into the server. Then a small loop waits until the page answers. 0.46 seconds. No computer starting up, no kernel loading, no boot: just one program starting. And curl, a tool that fetches a web address, gets the page: 200 OK.
time (docker run -d --name web -p 8080:80 nginx; until curl -s -m 1 -o /dev/null localhost:8080; do sleep 0.1; done)aadc4acce57c7a7ca03a3013586ba5e4333fb7b97f6ef3445198115eeb092832 real 0m0.458s user 0m0.013s sys 0m0.016s
curl -sI localhost:8080 | head -1HTTP/1.1 200 OK
Now the VM. web-vm is a virtual machine set up earlier with Ubuntu and nginx. virsh start boots it, and the same loop waits for the page. 9.9 seconds. To see where the time went, ssh logs into the VM, and a tool called systemd analyze reports its start-up: 3.6 seconds to start its kernel, 3.2 more to start the rest of Ubuntu, and only then nginx.
time (virsh start web-vm; until curl -s -m 1 -o /dev/null web-vm; do sleep 0.1; done)Domain 'web-vm' started real 0m9.930s user 0m0.043s sys 0m0.064s
ssh web-vm systemd-analyze | head -1Startup finished in 3.647s (kernel) + 3.185s (userspace) = 6.833s
Then start twenty more nginx containers, one after another. All twenty were running in 6 seconds, less than one VM takes to boot. A VM is a computer starting up. A container is a program starting up.

The kernel test
If containers really share the server's kernel, we can catch them at it. First, which kernel does the server itself run? It's Ubuntu 24.04, and uname dash r prints the kernel's version: 6 point 8. The kernel itself is just a file, kept in a folder called boot. There it is, highlighted.
grep PRETTY /etc/os-releasePRETTY_NAME="Ubuntu 24.04.5 LTS"
uname -r6.8.0-147-generic
ls /bootSystem.map-6.8.0-147-generic initrd.img vmlinuz config-6.8.0-147-generic initrd.img-6.8.0-147-generic vmlinuz-6.8.0-147-generic efi initrd.img.old vmlinuz.old grub lost+found
Now a container made from an older Ubuntu image, 20.04. An image is the set of files a container starts from. Inside, those files really do say 20.04. Guess first: which kernel will uname print? The server's, 6 point 8, the same version, to the last digit. And the boot folder inside the container is empty. There is no kernel in it at all.
docker run --rm ubuntu:20.04 grep PRETTY /etc/os-releasePRETTY_NAME="Ubuntu 20.04.6 LTS"
docker run --rm ubuntu:20.04 uname -r6.8.0-147-generic
docker run --rm ubuntu:20.04 ls -A /boot | wc -l0
Docker's own guide says it plainly: every container on a machine shares that machine's kernel. Run Alpine, a tiny Linux, in a container, and it reports the server's kernel too: six point eight.
Now Alpine as a virtual machine. virsh start boots it, and virsh console connects you to its screen, like a cable to a monitor. Log in, and ask for the kernel: six point eighteen. A different number from the server's six point eight: that's Alpine's own kernel. Same server, but this VM brought its kernel with it.
virsh start alpine-vmDomain 'alpine-vm' started
virsh console alpine-vmConnected to domain 'alpine-vm' Escape character is ^] (Ctrl + ]) Welcome to Alpine Linux 3.24 Kernel 6.18.60-0-virt on x86_64 (/dev/ttyS0) localhost login: root Welcome to Alpine! The Alpine Wiki contains a large amount of how-to guides and general information about administrating Alpine systems. See <https://wiki.alpinelinux.org/>. You can setup the system with the command: setup-alpine You may change this message by editing /etc/motd. localhost:~# uname -r 6.18.60-0-virt localhost:~# cat /etc/alpine-release 3.24.4 localhost:~# poweroff localhost:~#
One catch, so it doesn't fool you. Ask web-vm, the Ubuntu VM, for its kernel, and it prints 6 point 8 too. That's only because it runs the same Ubuntu release as the server. The number matches, but it's a second copy of the kernel, booted from the VM's own disk.

That's also why a container can only run programs built for the server's kernel. Ask Docker for a Windows image, and this Linux server refuses: there's no version of that image built for a Linux kernel. A VM can run Windows, because it brings its own kernel.
docker run --rm mcr.microsoft.com/windows/nanoserver:ltsc2022Unable to find image 'mcr.microsoft.com/windows/nanoserver:ltsc2022' locally docker: Error response from daemon: no matching manifest for linux/amd64 in the manifest list entries: no match for platform in manifest: not found Run 'docker run --help' for more information
What your server sees
So what keeps a container in its flat? The server's kernel shows it only its own things. Run ps, the command that lists running programs, inside a container. Every running program gets an ID number, and ps sees just one: its own, number one. The server, meanwhile, is running 244. These fences are called namespaces: they decide what a container is allowed to see.
docker run --rm alpine:3.24 psPID USER TIME COMMAND 1 root 0:00 ps
ps -e --no-headers | wc -l244
Now both are running nginx. pgrep lists the server's processes by name. Guess first: will the VM's nginx be on that list? No. Only the container's nginx shows up: nginx itself plus a few helper copies it starts, all ordinary programs on the server. Ask the VM itself, and its nginx is there, with ID numbers the server never sees.
pgrep -a nginx23448 nginx: master process nginx -g daemon off; 23519 nginx: worker process 23520 nginx: worker process 23521 nginx: worker process 23522 nginx: worker process
ssh web-vm pgrep -a nginx767 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; 768 nginx: worker process 769 nginx: worker process
To the server, the whole VM is one process: qemu, the program that runs a VM on KVM. It holds 527 megabytes: the VM's kernel, its Ubuntu, and its nginx, all inside. The container's nginx uses just 17 megabytes, and each of the twenty extra containers uses under 5.
ps -o pid,rss,comm -C qemu-system-x86_64PID RSS COMMAND 23562 539724 qemu-system-x86
docker stats --no-stream --format '{{.Name}}: {{.MemUsage}}' webweb: 17.09MiB / 7.755GiB
docker stats --no-stream --format '{{.Name}}: {{.MemUsage}}' app1 app2 app3app1: 4.777MiB / 7.755GiB app2: 4.773MiB / 7.755GiB app3: 4.777MiB / 7.755GiB
All together, the VM and twenty-one containers use about 1.3 gigabytes, and the one VM is most of that. Inside it, its own Ubuntu already uses 303 megabytes, before your app does anything. That's why one server fits dozens of containers in the memory of a few VMs.

Disk shows the same gap. The nginx image is 240 megabytes. The VM's disk is about 1.2 gigabytes: a 593 megabyte copy of Ubuntu, plus 585 megabytes of everything it has written since. A VM carries a whole operating system. A container carries one app and the files it needs.

How strong is the wall?
So containers are faster and lighter. What do you give up? Safety. Every container's wall is drawn by the same kernel. A bug in that kernel is a hole in every wall at once. A container talks to that shared kernel directly, through hundreds of system calls, the requests programs make to a kernel. A VM only talks to its fake hardware, a much smaller doorway, and an attacker inside would first have to break out of the VM's own kernel.

Docker's own guide agrees: containers are kept apart by a kernel they share, VMs by the hypervisor. So if you run code you don't trust, or code from other people, put it in a VM.
Which one, and when
So which one should your app run in? First, a surprise: you'll often use both. On a Mac, uname says Darwin: the Mac's own kernel. Inside a container there, it reports a Linux kernel: the one in a small Linux VM that Docker Desktop quietly starts for you. And cloud servers are usually VMs, running many containers each.
uname -srDarwin 27.2.0
docker run --rm alpine:3.24 uname -r7.0.14-linuxkit
So here's the one difference, in one line. A container shares the server's kernel. A VM brings its own kernel. Speed, size, memory and safety all follow from that.

So pick a container for one app that should start fast, and run as many copies as you need. Pick a VM when you need another operating system, like Windows. Pick a VM, too, for code you don't trust, or when you want a whole server to yourself. And often, the answer is both.

Wrap-up
If the next 'containers versus VMs' argument is easier now, subscribe, and we'll keep taking apart the things everyone assumes you already know.
The lab files and every command are linked in the description, so you can race the two on your own server. Thanks for watching Cloud Explainers.
Sources
Demos: a Linux server in our own lab (itself a VM with nested virtualization on), and our own Mac for Docker Desktop. Every command, output, size and time was real. Watch the full video on YouTube.