Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 111 ยท Getting Started

Docker vs Virtual Machines

Compare containers and virtual machines: isolation model, kernel sharing, startup time, resource use, security trade-offs and how to choose between them.

"Isn't a container just a small virtual machine?" Almost everyone asks this, and the honest answer is: it looks like one from the inside, but it is built on a completely different idea. A virtual machine (VM) pretends to be a whole computer, with its own kernel. A container is an ordinary process on the host that has been given a private view of the system and a budget of resources. Same host kernel, no pretend hardware.

That single difference explains almost everything people notice: why containers start in a blink, why they are small, why you can run dozens on a laptop, and why they are considered less strongly isolated. In this lesson you will compare the two models side by side, check the difference on your own machine with two commands, and learn how to choose (or combine) them.

Two ways to isolate software

A VM runs on a hypervisor, a thin layer of software (KVM, Hyper-V, VMware, VirtualBox) that carves the physical machine into virtual ones. Each VM gets virtual CPUs, a virtual disk and virtual network cards, and installs a complete operating system on top, kernel included. The applications inside have no idea they are not on real hardware.

A container engine does not emulate hardware at all. The host kernel is shared by every container, and the kernel itself separates them, using features you will meet in Namespaces, cgroups and Container Internals.

flowchart TB
    subgraph VM["Virtual machines"]
        direction TB
        A1["App A"] --> G1["Guest OS + kernel"]
        A2["App B"] --> G2["Guest OS + kernel"]
        G1 --> H["Hypervisor"]
        G2 --> H
        H --> HW1["Host OS / hardware"]
    end
    subgraph CT["Containers"]
        direction TB
        B1["App A + libraries"] --> E["Container engine"]
        B2["App B + libraries"] --> E
        E --> K["Host OS kernel (shared)"]
        K --> HW2["Hardware"]
    end

Each VM carries its own kernel; containers carry only an application and its libraries and share the host kernel.

Notice what a container image contains: the program, its libraries, a few tools and a root filesystem. It does not contain a kernel. When the program makes a system call (open a file, create a socket), it goes straight to the host's kernel, with no translation layer in between. That is why container performance is practically native.

See it for yourself

Run the same command on the host side and inside two different containers. This is real output from a Docker 29 installation:

CONSOLE
$ docker info --format '{{.KernelVersion}}'
6.18.54.1-microsoft-standard-WSL2

$ docker run --rm alpine uname -r
6.18.54.1-microsoft-standard-WSL2

$ docker run --rm python:3-slim sh -c 'grep PRETTY_NAME /etc/os-release; uname -r'
PRETTY_NAME="Debian GNU/Linux 13 (trixie)"
6.18.54.1-microsoft-standard-WSL2

The Debian-based image reports Debian as its "operating system", the Alpine image reports Alpine, yet all of them show the same kernel version, the one of the host. The "distribution" in a container is just a set of files (a userland), not a separate operating system. A VM would show its own kernel, and could even be a different OS family such as Windows.

One more surprise comes from the same experiment. Start a container with a memory limit, and ask it how much memory it has:

CONSOLE
$ docker run --rm -m 100m alpine sh -c 'head -1 /proc/meminfo; cat /sys/fs/cgroup/memory.max'
MemTotal:       12191876 kB
104857600

/proc/meminfo still shows the memory of the whole machine, because it is the host kernel answering. The limit (104,857,600 bytes, which is 100 MiB) is enforced elsewhere, in the cgroup. Programs that size themselves from /proc/meminfo can therefore misjudge their budget; modern runtimes such as the JVM, Go and .NET read the cgroup files instead.

On Windows and macOS, containers still need a Linux kernel. Docker Desktop therefore runs one lightweight Linux VM and puts all your containers inside it. That is why the kernel above is called microsoft-standard-WSL2 on a Windows machine. You get one VM, shared by all containers, not one VM per container. Docker Desktop, WSL2 and Colima covers those setups.

Comparing them

Virtual machine Container
What it virtualizes Hardware The operating system's view (processes, files, network)
Kernel Its own, per VM Shared with the host
Typical size Gigabytes (a full OS) Megabytes to a few hundred megabytes
Start time Tens of seconds (a boot) A fraction of a second to a few seconds
Memory overhead Guest OS memory, often reserved up front Only what the processes use
Density per host Tens Hundreds, sometimes thousands
Isolation Strong: a hypervisor boundary Good, but a kernel bug can cross it
OS flexibility Any OS on any host Same kernel family as the host (Linux containers need Linux)
Typical unit A "server" you patch for years An immutable image you replace

Some of these rows deserve a closer look.

Size and start time. The Alpine image shows 26.6MB in docker images on this machine, and python:3-slim about 184 MB. A server VM image is commonly several gigabytes, because it includes a kernel, init system, drivers and system tools. Starting a container is "create a process in new namespaces", which on a native Linux host takes well under a second. (Starting alpine true took about two seconds on this Windows laptop, because Docker Desktop adds a VM layer and a slow disk; the VM it runs in was already booted.) A VM must boot an OS first.

Density and efficiency. Ten VMs mean ten kernels, ten init systems and ten blocks of reserved memory. Ten containers mean ten processes. In practice, the same server can host many more containers, and the unused RAM of one is available to the others, because containers have only limits, while many VMs have reservations.

Isolation, honestly. A hypervisor presents a narrow hardware-like interface to the guest, which is hard to attack. Containers share a kernel with a very large surface (hundreds of system calls), so a kernel vulnerability can let a process escape its container. This does not make containers unsafe: seccomp filters, dropped capabilities, non-root users and read-only filesystems shrink that surface a lot (see Security Basics). It does mean you should think twice before running untrusted, hostile code (customers' arbitrary programs, for example) in plain containers on a machine that also holds secrets.

The software lifecycle. VMs grew up as pets: you log in, patch, tune and keep them for years. Containers encourage cattle: build an image once, run it anywhere, replace instead of repairing. A container is not a place you install things into; it is a running instance of an image you built from a Dockerfile.

When to choose which

Use containers when you:

  • ship your own applications and want identical behavior on a laptop, in CI and in production;
  • run many small services, scale them up and down quickly, or deploy several times a day;
  • want fast, cheap, disposable environments for builds and tests (Dev Containers);
  • trust the code and run Linux workloads on a Linux kernel.

Use VMs when you:

  • need a different operating system or kernel than the host (a Windows application on a Linux host, a custom kernel module, a specific kernel version);
  • need a strong boundary between tenants or between trust levels;
  • run software that expects a full machine: its own init system, many system services, kernel tuning or low-level device access;
  • are lifting and shifting a legacy application that cannot be repackaged.

Using both

Real systems usually combine them, and it is the norm, not a compromise:

  • Cloud providers sell VMs, and you run containers on those VMs. A Kubernetes or Swarm cluster is a group of VMs, each running a container engine.
  • Docker Desktop runs containers inside a VM, as seen above.
  • Sandboxed runtimes sit in between: gVisor intercepts system calls in user space, and Kata Containers and Firecracker wrap each container (or function) in a tiny VM. You keep the container workflow and gain hypervisor-level isolation. Several serverless platforms are built this way.
flowchart LR
    P["Physical server"] --> H["Hypervisor"]
    H --> V1["VM 1<br/>Linux + Docker"]
    H --> V2["VM 2<br/>Linux + Docker"]
    V1 --> C1["container"]
    V1 --> C2["container"]
    V2 --> C3["container"]
    V2 --> C4["container"]

A typical cloud setup: the VM is the isolation boundary between customers, containers are the packaging inside it.

Common misunderstandings

  • "A container is a lightweight VM." It is a process with restrictions. There is no guest kernel and no boot.
  • "Containers are always more secure/less secure." They are differently isolated. Configure them well and they are fine for ordinary workloads; for hostile code add a stronger boundary.
  • "I can run any OS in a container." Only the userland differs. A Linux container needs a Linux kernel, and a Windows container needs a Windows kernel (a different mode of Docker that this track does not cover).
  • "Containers store my data." The writable layer vanishes with the container, which is why volumes exist. A VM's disk, by contrast, persists by default.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. You run uname -r inside a Debian-based container and inside an Alpine-based container on the same host. What do you see?

    A container image has no kernel. All containers on a host use that host's kernel; the 'distribution' is only the set of userland files in the image.
  2. 2. Which statement about isolation is the most accurate?

    The hypervisor exposes a narrow, hardware-like interface. Containers share a kernel with a large system-call surface, which can be hardened but is not the same boundary.
  3. 3. Which workload clearly needs a VM rather than a plain container?

    Containers share the host kernel, so they cannot run a different operating system. A VM can host any OS regardless of the host.
  4. 4. Why do ten containers usually use much less memory than ten VMs running the same applications?

    Every VM needs its own kernel and system services. Containers are processes of the host with limits, not reservations.
  5. 5. How do most cloud-based container platforms combine the two technologies?

    Cloud servers are virtual machines; container engines run on top of them. The two layers solve different problems.

Summary

  • A VM virtualizes hardware and runs its own kernel; a container is an isolated process that shares the host kernel.
  • That is why containers are small, start almost instantly and pack densely, and why you cannot run a different OS kernel in one.
  • A container's "operating system" is only a userland of files; uname -r shows the host's kernel, and /proc/meminfo still shows the host's memory even under a limit.
  • VMs give stronger isolation and OS freedom; containers give speed, density and a clean build-and-replace workflow.
  • Production usually layers them: containers on VMs, or containers wrapped in micro-VMs for hostile workloads.
  • On Windows and macOS, Docker Desktop runs one Linux VM for all containers.

Next: Docker Desktop, WSL2 and Colima