Containers and virtual machines
In the previous lesson we found out that a container does not carry the system's kernel with it. To see what follows from that, let's compare it with a virtual machine: people often call a container "the same thing, only heavier", and that is wrong.
Back to the port. A virtual machine is a separate ship with its own engine and crew for each cargo. Containers are boxes on one shared ship: there is one engine, and each box has its own contents. The engine in this picture is the operating system's kernel, the part of the system that starts processes, hands them memory and gives access to files and the network. Every program asks the kernel for anything beyond its own memory.
The virtual machine
A virtual machine imitates a whole computer. A program called a hypervisor gives it processors, memory and a disk, and inside it a full guest operating system boots with its own kernel.
Three properties follow:
- starting takes tens of seconds: the guest system boots the way it does on a real computer;
- each machine keeps its own copy of a system in memory, so a server fits only a few of them;
- in return, any system can run inside: Windows on a Linux server, or Linux of another version.
The container
A container boots no system at all. It is an ordinary process on the host, the machine where Docker runs. The image carries only files: the app, libraries, utilities and the files of a distribution. All containers share one kernel, the host's.
Let's check. The uname -r command prints the kernel version, first on the host, then in a container from the Alpine Linux image:
uname -r
docker run --rm alpine:3.21 uname -r7.0.0-42-generic
7.0.0-42-genericThe versions match: the process in the container runs on the same kernel as the whole machine. Now let's ask which distribution each of them sees:
grep PRETTY_NAME /etc/os-release
docker run --rm alpine:3.21 grep PRETTY_NAME /etc/os-releasePRETTY_NAME="Ubuntu 24.04.4 LTS"
PRETTY_NAME="Alpine Linux v3.21"Let's walk through what happened:
- On the host, the file
/etc/os-releasebelongs to Ubuntu. - In the container, the same path leads into the image's files, and the Alpine file is there.
- The kernel is shared, but the container has its own system files: the image brought them.
On macOS and Windows, Docker Desktop starts one small Linux virtual machine, and all containers run on its kernel. So there a container shows that machine's kernel version, not the version of macOS or Windows, and it is the same in every container.
Comparison
| Virtual machine | Container | |
|---|---|---|
| Kernel | its own, the guest's | shared, the host's |
| Start | tens of seconds | a fraction of a second |
| Size | gigabytes | from a few megabytes |
| Isolation | stronger | weaker |
Numbers from my machine: a container from the image alpine:3.21 with the command true started, finished and was removed in 0.54 seconds. The image itself takes 7.79 MB.
A container's isolation is weaker for the same reason it is light. The kernel is shared by everyone, and a bug in the kernel can affect all containers on the host at once. A virtual machine has a hypervisor between the guest and the host.
This is where the ship picture stops working. Boxes on one ship cannot get into each other, but a process that finds a bug in the shared kernel can reach its neighbors.
When you still need a virtual machine
Containers do not push virtual machines out; they work together with them. A virtual machine is chosen when:
- the app needs another operating system or another kernel;
- one server runs code that cannot be trusted, such as programs of different customers;
- you need a whole separate server, with its own network and disks.
In the cloud, containers usually run exactly like that: inside virtual machines, many containers in each.
Common mistake
Calling a container a lightweight virtual machine. It leads to wrong expectations: another kernel for a container, a Windows image on a Linux host, isolation as strong as a separate machine's. None of this is true: a container is a host process, and it takes its kernel from the host.
Key points
- A virtual machine boots its own guest system with its own kernel; a container is a host process on the shared kernel.
- An image carries the files of the app and the distribution, but not a kernel.
- So a container starts in a fraction of a second, takes less space, and a server fits more of them.
- The shared kernel makes a container's isolation weaker than a virtual machine's.