Lesson 02/7 minutes/1 graded
Docker vs virtual machines
An operating system has two layers. Docker copies one of them and a virtual machine copies both, and every other difference follows from that.
Virtual machines have been around far longer than Docker, and both are called virtualization. So what is the difference, and why did Docker take over? The answer sits inside your operating system.
An operating system has two layers
Every operating system has a kernel and an application layer.
The kernel talks to the hardware. CPU, memory, disk, network card. When a program wants to read a file or open a port, it asks the kernel and the kernel deals with the hardware.
The application layer sits on top. Your shell, the system libraries, the package manager, the services you install, your code. It never touches hardware on its own. It goes through the kernel.
Both Docker and a virtual machine copy part of that stack. The difference is which part.
Docker copies the application layer and uses the kernel of the machine it is running on. A virtual machine copies both layers, so it brings its own kernel and does not touch yours.
Three things that follow from that
Size. A Docker image only holds the application layer, so images are measured
in megabytes. The nginx:1.30.4-alpine image on my laptop is 63.7 MB. A virtual
machine image holds a whole operating system, so it runs into gigabytes.
Speed. A virtual machine has to boot a kernel before it can do anything, which takes a minute or so. A container has no kernel to boot. The kernel starts it like any other process, so it is up in a moment.
Compatibility. This one is the catch. A virtual machine of any operating system runs on any host, so a Linux VM on Windows is fine. Docker cannot do that directly, because a Linux image needs a Linux kernel and Windows does not have one.
| Virtual machine | Container | |
|---|---|---|
| Kernel | Brings its own | Uses the host's |
| Image size | Gigabytes | Megabytes |
| Start up | Boots a kernel | Starts as a process |
| Runs any OS | Yes | Needs a Linux kernel |
How Mac and Windows get around it
Docker was built for Linux first. Almost every image you will use is a Linux image, so Docker later shipped Docker Desktop for Mac and Windows. It runs a small Linux system in a virtual machine on your computer and starts your containers inside that. The Linux kernel your containers need comes from there.
On Windows that Linux system is WSL 2 by default, or Hyper-V if you pick it during setup. On a Mac it is a virtual machine manager built for the job. You do not have to manage it. It is just useful to know it is there, because it explains why files shared from your Mac or Windows drive into a container feel slow, and why Docker Desktop shows memory use that no single container of yours accounts for.
When a virtual machine is still the better answer
You need a different kernel, like Windows containers on a Linux server. Or you are running code from people you do not trust, and a break out must not reach your machine. A container shares your kernel, so it does not give you that wall.
For everything else in this course, a container is the right tool.