SyntaxStudy
Sign Up
Docker Docker vs Virtual Machines
Docker Beginner 1 min read

Docker vs Virtual Machines

Virtual machines virtualise hardware: a hypervisor runs on bare metal and each VM contains a full OS kernel, system libraries, and the application. Containers virtualise the operating system: they share the host kernel and only package the application and its user-space dependencies. This makes containers orders of magnitude smaller (MBs vs GBs) and faster to start (milliseconds vs minutes). The trade-off is isolation strength. VMs provide hardware-level isolation with separate kernels, making them the choice for multi-tenant environments where hostile workloads must coexist. Containers rely on kernel namespaces and cgroups, which have had exploitable vulnerabilities. Production deployments of sensitive workloads often combine both: containers run inside dedicated VMs per tenant. For most application deployment scenarios — deploying your own code to infrastructure you control — containers provide sufficient isolation with dramatically better resource density. A single server can run dozens of containerised microservices that would each require a separate VM in a traditional deployment.
Example
# Spin up 5 nginx containers (each ~10 MB in memory)
for i in $(seq 1 5); do
  docker run -d --name web$i \
    --memory 32m --cpus 0.1 \
    -p $((8079+i)):80 \
    nginx:1.27-alpine
done

docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
docker stats --no-stream

# Clean up all running containers
docker stop $(docker ps -q)
docker rm   $(docker ps -aq)

# Image size comparison
docker pull ubuntu:24.04
docker pull alpine:3.20
docker pull node:20-slim
docker pull node:20-alpine
docker images --format "table {{.Repository}}:{{.Tag}}\t{{.Size}}"

# ubuntu:24.04   ~78 MB
# alpine:3.20    ~ 7 MB
# node:20-slim   ~90 MB
# node:20-alpine ~55 MB