Lesson 03 beginner

Docker vs Virtual Machines

How containers and virtual machines isolate workloads, what each costs in size, speed and security, and why most clouds run containers on top of VMs.

Containers and virtual machines both let you run several isolated workloads on one physical server. They do it at different layers, and that one difference explains their size, speed and security trade-offs.

Two ways to isolate

A virtual machine emulates a whole computer. A hypervisor (KVM, VMware ESXi, Hyper-V) gives each VM virtual CPUs, memory and disks, and every VM boots its own operating system kernel.

A container virtualizes the operating system instead of the hardware. All containers on a host share the host’s kernel; the kernel only gives each one its own view of processes, network, mounts and users.

LayerVirtual machineContainer
Your app and librariesInside the guest OSInside the image
Operating system kernelOne per VMShared with the host
Isolation provided byHypervisor and hardware virtualizationKernel namespaces and cgroups
Typical sizeGigabytesMegabytes
Start timeTens of seconds to minutesMilliseconds to a few seconds
Density per serverTensHundreds or more

See the shared kernel yourself

Run the same command on the host and inside two very different distributions:

$ uname -r
6.8.0-48-generic
$ docker run --rm alpine uname -r
6.8.0-48-generic
$ docker run --rm ubuntu:24.04 uname -r
6.8.0-48-generic

The Alpine and Ubuntu images bring their own files and package managers, but the kernel version is always the host’s. A VM would report its own kernel.

You can also see that a container is just a process on the host:

$ docker run -d --name sleeper alpine sleep 600
$ ps -eo pid,cmd | grep "sleep 600" | grep -v grep
 48213 sleep 600
$ docker rm -f sleeper
sleeper

Namespaces and cgroups

Two Linux kernel features do the work behind every container:

  • Namespaces decide what a process can see: its own process tree (PID), network interfaces (NET), mount points (MNT), hostname (UTS), users (USER) and inter-process communication (IPC).
  • Control groups (cgroups) decide how much a process can use: CPU time, memory, disk I/O and the number of processes.

Docker adds a few more safety layers on top, including dropped Linux capabilities, a default seccomp profile that blocks dangerous system calls, and optional AppArmor or SELinux policies.

Security trade-offs

A VM’s boundary is the hardware virtualization layer, which is a small and well-tested attack surface. A container’s boundary is the shared kernel, so a kernel vulnerability can, in the worst case, let a process escape to the host.

In practice this means:

  • Running trusted workloads of one team or customer in containers is standard practice.
  • Hostile multi-tenant workloads, such as running strangers’ code, usually get an extra boundary: one VM per tenant, or sandboxed runtimes like gVisor or Kata Containers.
  • Containers should never run as root without a reason, and should not get --privileged (lesson 8 shows how to set a non-root user).

Important: docker run --privileged and mounting /var/run/docker.sock into a container both give it near-complete control of the host. Treat them like handing over root access.

Better together

You rarely have to choose. Cloud providers sell VMs, and you run containers inside them: the VM isolates you from other customers, and containers let you pack many services onto each VM. Kubernetes nodes, for example, are usually VMs running dozens of containers each.

Choose a VM when you needChoose a container when you need
A different kernel or operating system (Windows on Linux)Fast, repeatable deploys of server software
Strong isolation between untrusted tenantsMany small services on few servers
Full control of drivers and kernel modulesThe same artifact from laptop to production

Exercise

Which Linux kernel feature limits how much memory a container can use?

  1. Namespaces
  2. chroot
  3. cgroups
  4. seccomp
Show answer

Answer: C. Namespaces control what a process can see, cgroups control how much it can use: CPU, memory and I/O. docker run --memory is a cgroup setting.

Key takeaways

  • VMs virtualize hardware and run their own kernel; containers share the host kernel.
  • Namespaces isolate what a process sees; cgroups limit what it uses.
  • Containers are smaller and faster, while VMs give a stronger security boundary.
  • In the cloud you usually run containers inside VMs and get both benefits.

FAQ

Can I run Windows containers on Linux?

No. Windows containers need a Windows kernel, and Linux containers need a Linux kernel. Docker Desktop on Windows runs Linux containers inside a small Linux VM.

Are containers less secure than VMs?

Their isolation boundary is thinner, so they need more care: non-root users, no privileged mode, up-to-date kernels and minimal images. For trusted workloads, a well-configured container is secure enough for production.

Do containers replace virtual machines?

Not entirely. They replaced VMs as the unit of deployment for most applications, but VMs remain the foundation underneath, especially in the cloud.

↑ ↓ navigate Enter open Esc close