Docker vs. Virtual Machines: The Container Mental Model Explained
Docker vs virtual machine: why containers share the host kernel, what namespaces and cgroups isolate, and when a VM is the better choice.

“A container is a lightweight VM” is the most common explanation of Docker, and it is the source of most early confusion. People SSH into containers, install cron and a logging agent inside them, expect them to boot with init systems, and are surprised when a kernel module loaded on the host appears inside every container. None of that matches a VM, because a container is not a small machine. It is a process with a restricted view of the machine it runs on.
A virtual machine runs on a hypervisor, such as KVM, VMware ESXi or Hyper-V, that presents virtual hardware: CPUs, memory, disks and network cards. A full guest operating system, kernel included, boots on that virtual hardware. A container runs directly on the host’s kernel. Docker, through its runtime, starts a normal process but places it in its own namespaces, so it sees its own filesystem, process tree, network stack and hostname, and in cgroups, which cap how much CPU, memory and I/O it can use.
The layers, side by side
Virtual machines Containers
+-------------------------------+ +-------------------------------+
| App A | App B | App C | | App A | App B | App C |
| Libs | Libs | Libs | | Libs | Libs | Libs |
| Guest | Guest | Guest | | (image filesystems only) |
| kernel | kernel | kernel | +-------------------------------+
+-------------------------------+ | Container runtime (Docker) |
| Hypervisor | +-------------------------------+
+-------------------------------+ | Host OS and its single kernel |
| Hardware | +-------------------------------+
+-------------------------------+ | Hardware |
+-------------------------------+
Each VM carries a whole operating system: kernel, init system, system services and often a few gigabytes of disk. Each container carries only the application and the user-space files it needs, such as libraries, a language runtime and certificates. There is exactly one kernel on the container side, and every container uses it.
What actually isolates a container
Linux has had the building blocks for years. Docker’s contribution was packaging them into a developer-friendly workflow.
| Kernel feature | What it gives the container |
|---|---|
| PID namespace | Its own process tree; the app runs as PID 1 and cannot see host processes |
| Mount namespace | Its own root filesystem, built from the image layers |
| Network namespace | Its own interfaces, IP address, routing table and ports |
| UTS namespace | Its own hostname |
| IPC namespace | Its own shared memory and message queues |
| User namespace | Optional mapping of container root to an unprivileged host user |
| cgroups | Limits and accounting for CPU, memory, I/O and process count |
| Capabilities, seccomp, AppArmor or SELinux | Restrictions on which privileged operations and system calls it may use |
You can see this from the host. Run docker run -d nginx and then ps aux | grep nginx on the host: the nginx processes appear in the host’s process list like any other program. Inside the container they look like the only processes in the world. That is the mental model to keep: same kernel, different view.
The practical differences
| Property | Virtual machine | Container |
|---|---|---|
| Startup time | Tens of seconds to minutes (boots an OS) | Milliseconds to a few seconds (starts a process) |
| Size on disk | Gigabytes per image | Megabytes to a few hundred megabytes, with shared layers |
| Memory overhead | A full OS per VM | Close to the application’s own usage |
| Density per host | Tens of VMs | Hundreds of containers |
| Kernel | Its own; any OS the hypervisor supports | The host’s; Linux containers need a Linux kernel |
| Isolation boundary | Hardware virtualization; strong | Kernel features; weaker by design |
| Typical unit | A server | A single process or service |
| Lifecycle | Long-lived, patched in place | Disposable, replaced by a new image |
The last row is the cultural shift. VMs are usually treated as pets: logged into, patched and maintained for years. Containers are treated as cattle: you never patch a running container, you build a new image and replace it. Logs go to stdout, data goes to volumes, and configuration comes from the environment, so that any container can be thrown away at any moment.
“But Docker runs on my Mac”
Linux containers need a Linux kernel. Docker Desktop on macOS and Windows quietly runs a small Linux virtual machine and runs your containers inside it. That is why file sharing between your Mac and a container is slower than on Linux, why containers cannot see your Mac’s processes, and why Docker Desktop has a memory setting: it is the size of that hidden VM. On Windows, WSL 2 provides the Linux kernel. Windows containers, which run Windows applications on a Windows kernel, exist too, but they are a separate world with separate images.
It is also why an image built on an Apple Silicon Mac defaults to the arm64 architecture and may not run on an amd64 server. The image contains compiled user-space programs, and they must match the CPU architecture of the kernel that runs them.
Security: a shared kernel is a shared risk
Because all containers share one kernel, a kernel vulnerability that allows escaping a namespace affects every container on the host. A VM escape requires breaking the hypervisor, which has a far smaller attack surface. That does not make containers insecure; it means the defaults matter:
- Do not run containers with
--privileged, which removes most of the isolation. - Run processes as a non-root user inside the container.
- Keep the default seccomp profile and drop capabilities you do not need.
- Do not mount the Docker socket into a container unless you understand that it grants control of the host.
For running untrusted code from different customers on the same hardware, combine the two models. Cloud providers run each customer’s containers inside lightweight VMs, as AWS does with Firecracker for Lambda and Fargate, and projects such as Kata Containers and gVisor add a VM or user-space kernel boundary around individual containers.
When a VM is still the right tool
- You need a different kernel or operating system, such as running Windows software on Linux hosts, or testing a kernel module.
- You need strong isolation between tenants who do not trust each other.
- The workload is a whole machine, such as a legacy application that expects systemd, multiple daemons and a persistent hostname.
- Licensing or compliance is defined per machine.
And containers are the better tool when you want reproducible builds that run identically on a laptop, CI runner and production server; fast startup for scaling and deployments; and high density for many small services.
They are not rivals
In practice almost every production container runs inside a VM: an EC2 instance, a GCP Compute Engine VM or an Azure VM running a container host. The VM provides isolation from other cloud customers and a stable machine to manage. Containers provide the packaging and deployment model for the applications on top. The question is rarely “Docker or VMs?” and almost always “which boundary belongs at which layer?”
Frequently Asked Questions
Is a Docker container a virtual machine?
No. A virtual machine runs a full guest operating system with its own kernel on virtual hardware. A container is a group of normal processes on the host, isolated by Linux namespaces and limited by cgroups, sharing the host’s kernel.
Why do containers start faster than virtual machines?
Starting a container only starts a process with a prepared filesystem and isolation settings. Starting a VM boots an entire operating system, including the kernel and system services, which takes far longer.
Are containers less secure than virtual machines?
Their isolation boundary is weaker, because all containers on a host share one kernel. With sensible defaults such as non-root users, no privileged mode and seccomp profiles, containers are safe for most workloads. For untrusted multi-tenant code, run containers inside VMs or sandboxed runtimes.
Why does Docker Desktop need a virtual machine on Mac and Windows?
Linux containers require a Linux kernel. Docker Desktop runs a small Linux VM on macOS, and uses WSL 2 or Hyper-V on Windows, and the containers run inside it.
Keep the right picture in mind
Think of a container as a process with blinders on, not a tiny server. That picture predicts how it starts, why it is small, why it shares the kernel’s strengths and weaknesses, and why you replace it instead of fixing it. With it in place, the rest of Docker, from images and layers to volumes and networks, is just a set of tools for preparing and running those processes well. For the full reference, see the Docker overview and the Docker Engine security documentation. Related reading on this site: Docker architecture and Docker images, containers and layers.
Originally posted on 11 January 2023 and post last modified on 11 January 2023