Sub topic 14
Choosing between containers and VMs
This chapter assumes you know the earlier material: a container is an isolated process that shares the host kernel, and a VM runs its own guest kernel on virtualized hardware. It also assumes you are comfortable with the basic security trade-offs between the two. Here you turn that knowledge into a decision.
Start with the questions that matter
No option wins in every case. Ask these questions in order, because the first few often decide the matter.
- Do you trust the code? Code from other tenants, or code you did not write, needs a strong boundary. A kernel bug can let a container escape, so a VM boundary is the safer default.
- Which kernel do you need? Containers use the host kernel. A Linux container cannot run a Windows program, and it cannot load a custom kernel module of its own. A different OS or kernel version means a VM.
- How fast must it start and how dense must it be? A container starts in well under a second and carries little memory overhead. A VM boots a whole OS, which takes longer and reserves more memory.
- How portable must the package be? An image runs the same on any host with a compatible kernel and runtime. A VM image is larger and tied to a hypervisor's disk format.
Decision table
| Situation | Better fit | Reason |
|---|---|---|
| Many small stateless services from one team | Containers | Fast start, high density, one image per service |
| Running untrusted customer code | VM or microVM | Separate kernel limits the damage of an escape |
| Windows app on a Linux host | VM | Containers cannot supply a different kernel |
| Legacy app that needs a specific OS version | VM | The guest OS is part of the package |
| CI jobs that start and stop constantly | Containers | Low startup cost |
| Strict compliance that requires hardware-level separation | VM | A hypervisor gives a stronger boundary |
A combined approach is common
Most real systems use both. Cloud providers run your containers inside VMs, so the VM is the boundary between customers and containers are the unit of deployment inside it. When you need stronger separation per workload, microVMs and sandboxed runtimes give each container its own kernel or kernel-like layer, at some cost in startup time and overhead.
Common mistakes
- Treating a container as a security boundary equal to a VM. Hardening helps, but the kernel is still shared.
- Choosing a VM only out of habit. If the workload is trusted and runs on Linux, a VM per service wastes memory and slows deployment.
- Forgetting the host kernel on macOS and Windows. Containers there run in a hidden Linux VM, so your local results show VM overhead that a Linux server would not have.
- Ignoring state. Container filesystems are disposable by default. Persistent data needs a volume or an external service in either model.
Check the kernel yourself
Before choosing, confirm what a container really shares with its host. On a Linux host, both commands print the same release.
uname -r
docker run --rm alpine uname -rIf the two outputs match, the container has no kernel of its own. That fact drives most of the trade-offs above.
Exercise
A company hosts a platform where customers upload and run their own Python scripts. The platform runs on Linux. Which approach would you pick, and why?
Show answer
Use a VM or a microVM per customer, or a sandboxed container runtime. The scripts are untrusted, and plain containers share the host kernel, so one kernel exploit could expose other customers. A separate kernel gives a stronger boundary. Containers can still package the scripts inside that boundary.
Exercise
A team runs 40 trusted internal Linux services and wants fast deployments and high density on each server. What do you recommend?
Show answer
Containers. The code is trusted, the kernel requirement is the same for every service, and containers start quickly with low memory overhead. Running the container hosts as VMs in the cloud is fine.