Virtualization & Containers
Virtualization splits one physical machine into many isolated guests; containers split one kernel into many isolated processes. Learn hypervisors, how VMs and containers differ, and how orchestration sits on top of both.
What is this?
Virtualization lets one physical server run many operating systems. A hypervisor (KVM, ESXi, Hyper-V) multiplexes hardware across virtual machines, each with its own kernel. Containers go one level higher: they share the host kernel but isolate processes with namespaces and cgroups, packaging an app and its dependencies into a portable unit. Orchestrators like Kubernetes manage fleets of both at scale.
Why do you need it?
- Every cloud instance you rent is a virtual machine running on a shared physical host.
- Containers changed how applications are built, shipped and scaled — you will live in them daily.
- Understanding the isolation boundary decides what breaks when a neighbor on the host misbehaves.
- Capacity planning, security and cost all trace back to whether you run VMs, containers or bare metal.
- Kubernetes, the orchestration layer, exists because running containers by hand does not scale.
Mental model
Apartments in a building
Virtual machines and containers
A physical server is the building. A VM is a fully separate apartment with its own walls, plumbing and electricity — heavy, but totally isolated. Containers are roommates in one apartment sharing the kitchen (kernel) but with their own locked rooms. Roommates are cheaper and start instantly, but the kitchen is a shared, contended resource.
How it works
- The hypervisor sits between hardware and guests, virtualizing CPU, memory, storage and interrupts so each VM believes it owns the machine.
- Each VM runs a full operating system with its own kernel, system libraries and boot lifecycle — which is why VMs take minutes to start.
- A container reuses the host kernel but requests an isolated view of it through namespaces (PID, network, mount, user) and resource limits through cgroups.
- The container image is a filesystem snapshot plus metadata, built from layers and described by the OCI runtime spec; the runtime (containerd, runc) unpacks and starts it.
- Orchestration (Docker Swarm, Kubernetes) schedules containers across hosts, restarts failures, does rolling updates and handles service discovery.
VMs and containers side by side on the same host.
Key commands
docker psList running containers with their image, status and published ports.
docker run -d --name web -p 8080:80 nginx:1.25Start an nginx container detached, mapping host port 8080 to container port 80.
docker imagesList locally cached container images.
docker logs webPrint stdout/stderr from a container — the primary debugging channel.
free -mSee host memory; containers share this pool and do not show their own separate RAM.
Examples
Run an nginx container
$ docker run -d --name web -p 8080:80 nginx:1.257a9f3c1b5d2e4f6a8b0c2d4e $ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES7a9f3c1b5d2e nginx:1.25 "/docker-entrypoint..." 5 seconds ago Up 4 seconds 0.0.0.0:8080->80/tcp web $ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080200docker run pulls the image, creates a container with its own PID, network and mount namespaces, and maps host port 8080 to the container's port 80. The curl confirms the web server answers. The container started in under a second because no kernel was booted — it reuses the host's.
In production
- Autoscaling in the cloud is largely VM orchestration: launch instances, join them to a load balancer, and terminate them on scale-in.
- Containers give you the same artifact in dev, CI and production — the image is immutable.
- Kubernetes runs container fleets with declarative desired state, self-healing and rolling deployments.
- Serverless platforms and managed databases are themselves built on VMs and containers under the hood.
Best practices
- Keep images small and layered — order stable base layers first so rebuilds hit the cache.
- Run one concern per container, but remember that one process per container is a guideline, not a law.
- Pin image tags to digests or explicit versions; floating latest breaks reproducibility.
- Set memory and CPU limits at every layer (cgroups, Kubernetes resources) so one tenant cannot starve the host.
Common mistakes
- Treating containers as lightweight VMs — containers share the kernel and cannot run a different OS.
- Storing state inside a container's writable layer, which dies with the container.
- Running a process manager inside a container instead of letting the orchestrator manage restarts.
- Assuming VM-level isolation is enough for hostile multi-tenancy — containers give weaker isolation between neighbors.
Common issues
docker: command not foundThe Docker CLI is not installed or not on PATH.
Install the engine or verify PATH with which docker; the daemon must be running too (docker info).
Error response from daemon: port is already allocatedHost port 8080 is already bound by another container or process.
Pick a free port or find the holder with docker ps and free it.
Container exits immediately with code 137The process was killed — almost always by the OOM killer exceeding a memory limit.
Raise the memory limit or fix the memory leak; verify with docker inspect and dmesg.
Debugging tips
- docker logs and docker inspect are the first stop for any container failure.
- Compare free -m on the host against container limits to find cgroup memory throttling.
- Use docker ps -a to see exited containers — many failures hide in the stopped list.
- Check resource limits before blaming the app: OOM-killed processes exit 137.
Interview checkpoint
Hands-on lab
- Confirm the host OS and kernel details
- Run an nginx container with a port mapping
- Verify the container's published port
- Read host resource state alongside the container
Run a Container and Observe Resource Isolation
25 min · intermediate
Quick quiz
Which component shares the host kernel with other workloads?
Which technology virtualizes the hardware so multiple operating systems run on one machine?
Containers provide the same level of OS isolation as virtual machines.
Cheat sheet
VMFull guest OS + kernel, minutes to boot, strong isolationContainerShares host kernel, milliseconds to start, portable imageHypervisorKVM, ESXi, Hyper-V — virtualizes hardwareRuntimecontainerd + runc — unpacks and starts OCI imagesOrchestratorKubernetes — schedules, heals, scales containersdocker ps -aAll containers, including exited onesdocker run -d -p 8080:80 nginxDetached run with port mappingdocker logs <name>See stdout/stderrdocker inspect <name>JSON details: mounts, limits, statedocker rm -f <name>Force remove a container