
·7 min read
Docker - Virtual Machines vs Containers Comparison
What a VM and a container actually are under the hood, where each one hurts, and how I decide which one a workload gets.
"Should we use VMs or containers?" It's the wrong question. The real question is: what are you trying to solve?
I've run infrastructure with both VMs and containers, and here's the thing nobody tells you: they're not competing technologies. They solve different problems. But everyone acts like you have to pick a side.
Let me tell you what actually matters.
Bottom line: VMs give you a whole computer inside your computer (slow, heavy, isolated). Containers give you just your app and its dependencies (fast, light, share the kernel). Use VMs when you need real isolation or different operating systems. Use containers when you want speed and density. Or use both. That's what most of us do.
VMs: The Computer Inside Your Computer
Virtual machines are basically "let's pretend we have 10 servers when we actually have 1."
When you boot a VM, you're booting an entire operating system. It has its own kernel, its own drivers, its own everything. The hypervisor (VMware, Hyper-V, KVM) tricks the VM into thinking it's running on real hardware.
I remember the first time I saw VMware ESXi take a single physical server and run 20 Windows Servers on it. It felt like magic. All those years of "one app, one server" suddenly seemed absurd.
When VMs Make Sense
- Real isolation: You're running untrusted code, or a multi-tenant setup where one customer absolutely cannot affect another.
- Different operating systems: You need Windows and Linux on the same hardware. Windows containers don't run on a Linux kernel at all, so this one isn't up for debate.
- Legacy apps: That ancient Java app that only runs on RHEL 5? Throw it in a VM and forget about it.
- Kernel-level work: Anything that needs its own kernel version, kernel modules, or sysctls you don't want to touch on a shared host.
When VMs Hurt
- Boot time: 30 seconds to 5 minutes. Try scaling that quickly when traffic spikes.
- Size: A minimal Linux VM disk is 500MB-1GB, a typical one is 10GB+. Multiply by 50 VMs and you're storing the same OS files 50 times.
- Overhead: Every VM runs a full kernel and its own init, logging, cron, agents. That's RAM and CPU doing nothing but keeping the lights on.
- Patching: Security update drops? Hope you enjoy patching 100 different OS instances.
I once managed a cluster where we had 200 CentOS VMs. When Heartbleed hit, I spent a week doing nothing but patching and rebooting VMs.
The honest footnote: containers would have helped less than people think. Heartbleed was an OpenSSL bug, and OpenSSL lives in userspace, inside every image. With containers you don't reboot 200 machines, but you do rebuild and redeploy every image that ships the vulnerable library. Kernel CVEs are the ones you really patch once, on the host. Userspace CVEs follow the image. That's why image scanning in CI matters more than people give it credit for.
Containers: Just a Process With Blinders On
A container isn't a small VM. It's a normal Linux process that the kernel lies to:
- Namespaces decide what the process can see: its own PID tree, network stack, mounts, hostname, users.
- cgroups decide how much it can use: CPU, memory, PIDs, I/O.
- The image is a stack of filesystem layers that becomes the process's root filesystem.
There's no guest kernel. Every container on a host shares the host's kernel. You can see this yourself on any Linux box:
# Same kernel version inside and outside the container
uname -r
docker run --rm alpine uname -r
# The container's process is just a process on the host
docker run -d --name sleeper alpine sleep 1000
ps aux | grep "sleep 1000"
docker rm -f sleeperOn a Mac or Windows laptop the uname -r inside the container will show a different kernel than your OS. That's because Docker Desktop quietly runs a small Linux VM and your containers live inside it. So if you use Docker Desktop, you're already running containers on a VM every day.
cgroups are also why limits actually bite. Give a container 256MB and let it allocate more, and the kernel kills it:
# tail buffers /dev/zero looking for a newline that never comes
docker run --rm --memory=256m alpine tail /dev/zero
echo $? # 137 = killed by SIGKILL (OOM)That exit code 137 is the same thing you see as OOMKilled in Kubernetes.
Where Containers Hurt
- Shared kernel: A kernel exploit inside one container is a kernel exploit on the host. A VM escape is a much rarer event than a container escape.
- Root is root: Without user namespaces, UID 0 in the container is UID 0 on the host. Run
--privilegedor mount/var/run/docker.sockinto a container, and you've effectively handed it the host. I treat both as "needs a reason in the PR". - Linux only (mostly): Linux containers need a Linux kernel. Windows containers need a Windows host.
- Operations at scale: One host with
docker runis easy. Fifty services across ten nodes means you need an orchestrator, and that's a whole new system to run.
Side by Side
The Stacks
Virtual Machine Stack
┌─────────────────────────────────────────────────┐
│ Application │
├─────────────────────────────────────────────────┤
│ Guest OS (Linux/Windows) │
├─────────────────────────────────────────────────┤
│ Hypervisor (VMware/Hyper-V/KVM) │
├─────────────────────────────────────────────────┤
│ Host OS (if Type 2) │
├─────────────────────────────────────────────────┤
│ Physical Hardware │
└─────────────────────────────────────────────────┘Container Stack
┌─────────────────────────────────────────────────┐
│ Application │
├─────────────────────────────────────────────────┤
│ Container Runtime (Docker/containerd) │
├─────────────────────────────────────────────────┤
│ Host OS + shared kernel (Linux) │
├─────────────────────────────────────────────────┤
│ Physical Hardware │
└─────────────────────────────────────────────────┘The whole difference is that Guest OS layer. Everything in the tables below follows from it.
Rough Numbers
| Metric | Virtual Machines | Containers |
|---|---|---|
| Startup time | 30 seconds to minutes | Under a second to a few seconds |
| Memory overhead | Hundreds of MB to GBs per VM (full OS) | A few MB per container |
| Disk | 1-100+ GB per VM | 5 MB (alpine) to a few GB (CUDA images) |
| Density | Tens per host | Hundreds per host (Kubernetes defaults to 110 pods per node) |
| CPU performance | Near-native with hardware virtualization | Native |
These are orders of magnitude, not benchmarks. A fat container image with a slow app can start slower than a tuned microVM.
Security, Plainly
A VM boundary is a hypervisor, which is a small, heavily audited attack surface. A container boundary is the entire Linux syscall interface. That's the whole argument.
My rule: if I don't trust the code (customer code, CI jobs from forks, multi-tenant workloads), it gets a VM or a sandboxed runtime. If it's our own code, containers with sane defaults (non-root user, no privileged, dropped capabilities, read-only root filesystem where possible) are fine.
What Containers Look Like in Practice
The place containers really pay off is the build-ship-run loop. A small Compose file to run services locally:
services:
user-service:
image: user-service:latest
ports:
- "3001:3000"
depends_on:
- mongodb
product-service:
image: product-service:latest
ports:
- "3002:3000"
depends_on:
- postgresAnd the same image going through CI, built once and tested exactly as it will run:
name: CI
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build Docker image
run: docker build -t myapp:${{ github.sha }} .
- name: Run tests
run: docker run --rm myapp:${{ github.sha }} npm testTry doing that with VM images on every push. You can (Packer exists), but nobody enjoys it.
A Dockerfile that keeps the image small and doesn't run as root:
FROM node:22-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
FROM node:22-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY . .
USER node
EXPOSE 3000
CMD ["node", "server.js"]CMD ["node", ...] instead of npm start so the Node process is PID 1 and actually receives SIGTERM when the container is stopped. With npm start in the middle, you often wait the full 10-second grace period and then get SIGKILLed.
Using Both
Containers on VMs
This is what almost everyone actually runs. Every managed Kubernetes (EKS, AKS, GKE) is containers on VMs. The VM is the hard boundary (per environment, per tenant, per node pool), and containers are how you pack apps inside it.
Containers With a VM Inside
If you need container ergonomics but VM-grade isolation, there are runtimes for that:
- Kata Containers: each pod runs inside a lightweight VM with its own kernel.
- gVisor: a user-space kernel (
runsc) that intercepts syscalls, so the container never talks to the host kernel directly. GKE Sandbox is built on it. - Firecracker: microVMs that boot in a fraction of a second. AWS Lambda and Fargate run on it.
In Kubernetes you pick these per workload with a RuntimeClass, so trusted services stay on plain runc and only the risky stuff pays the overhead.
How I Decide
- Untrusted code or hard multi-tenancy? VM, or a sandboxed runtime like gVisor/Kata.
- Needs Windows, a specific kernel, or kernel modules? VM.
- Legacy app that nobody wants to touch? VM. Containerizing it is a project, not a weekend.
- Our own services, deployed often, scaled up and down? Containers, on VMs.
Pick the one that matches the problem in front of you. Don't pick based on what's trending.
A note on how this was written: I used AI to help with phrasing and structure. The tone, the opinions, the war stories, the code, and everything else here are mine.