123DevOps··7 min read

How does a container work? Containers vs virtual machines vs serverless

See what happens under the hood of a container — namespaces, cgroups, overlayfs, OCI specs, and runtimes — with safe demos, and compare containers, virtual machines, and serverless to pick (or combine) the right model.

Containers

Many people picture a container as a “small virtual machine.” The comparison helps on day one but gets in the way later: a container has no kernel of its own. It is an ordinary Linux process with a restricted view of the system and resource limits. That explains why containers start so fast, why they isolate less than a VM, and where serverless fits into the story.

What a container is on the inside

A container is built from features the Linux kernel already provides. There is no “container” object in the kernel; instead, there is a combination of three pieces.

Namespaces: what the process sees

Namespaces give a process an isolated view of parts of the system:

  • pid: sees only its own processes; the first one becomes PID 1;
  • net: its own interfaces, IPs, routes, and ports;
  • mnt: its own mount points (the file system it sees);
  • uts: its own hostname;
  • ipc: isolated queues and shared memory;
  • user: lets it be “root” inside without being root on the host;
  • cgroup: hides the host’s cgroup hierarchy.

Cgroups: how much the process can use

Cgroups (control groups) limit and measure CPU, memory, disk I/O, and the number of processes. They are why the kernel kills a container that blows past its memory limit (the OOM kill).

Layered file system

An image is a set of read-only layers (base system, dependencies, application). When the container runs, overlayfs stacks those layers and adds a writable layer on top. Ten containers from the same image share the lower layers, saving disk space and time.

OCI specs and runtimes

The OCI (Open Container Initiative) defines open specifications, mainly:

  • Image Spec: the image format;
  • Runtime Spec: how a runtime must create and run the container.

Low-level runtimes such as runc (the reference implementation) and crun create namespaces, configure cgroups, and start the process. Above them, high-level runtimes such as containerd and CRI-O pull images, manage the lifecycle, and serve Kubernetes. Docker uses containerd under the hood.

Seeing isolation in practice

With Docker installed, run a container with a memory limit:

docker run --rm -it --memory=256m --name demo alpine sh

# inside the container:
ps aux        # only sh and ps itself show up; sh is PID 1
hostname      # its own name (by default, the container's short ID)
cat /sys/fs/cgroup/memory.max   # 268435456 (256 MiB) on cgroup v2 hosts

In another terminal, while the container is running:

docker stats --no-stream demo                         # current usage and memory limit
docker inspect --format '{{.HostConfig.Memory}}' demo # 268435456
docker inspect --format '{{.State.Pid}}' demo         # PID of the same process as seen by the host

The last command shows the key point: on the host, the container is a process with a different PID. On macOS and Windows, Docker Desktop runs a hidden Linux VM, because Linux containers need a Linux kernel.

On a Linux machine, you can see a namespace without Docker at all:

sudo unshare --pid --fork --mount-proc bash
ps aux    # bash shows up as PID 1; the host's processes disappear
exit      # back to your normal shell

unshare creates a PID namespace and mounts its own /proc; when you exit, it goes away. Still, prefer to test on a lab VM.

Virtual machines

A virtual machine emulates an entire computer. A hypervisor splits the hardware among several VMs, and each VM runs its own kernel and a full operating system.

  • Type 1 (bare metal): runs directly on the hardware. Examples: VMware ESXi, Microsoft Hyper-V, and KVM, which is built into the Linux kernel and usually classified in this category.
  • Type 2 (hosted): runs as a program on top of another operating system. Example: VirtualBox.

The result is strong isolation — the boundary is the virtualized hardware, not a shared kernel — at the cost of more memory, more disk, and slower boot. VMs also let you run Windows and Linux side by side on the same host.

Serverless

With serverless, you hand over code or an image and the provider takes care of servers, scaling, and updates (see what is cloud computing). There are two flavors:

  • Functions (FaaS): AWS Lambda, Azure Functions, and Google Cloud Run functions (formerly Cloud Functions). The function reacts to events: an HTTP request, a queue message, a new file.
  • Serverless containers: Google Cloud Run, AWS Fargate, and Azure Container Apps. You hand over a container image and don’t manage nodes.

What they share: pay-per-use (requests, time, and memory), automatic scaling — often down to zero — and cold starts, the delay on the first request when no instance is ready. There are also limits on execution time (30 minutes on Lambda since November 2026, up from 15), memory, and package size. Event-driven scaling is also possible on Kubernetes, with KEDA.

The middle ground: microVMs and sandboxed runtimes

The line between containers and VMs has become blurrier:

  • Firecracker: an open source microVM manager from AWS, used in Lambda. Each function runs in a minimal VM, with its own kernel and a very fast boot.
  • gVisor: a user-space “kernel” from Google that intercepts the container’s system calls before they reach the host kernel.
  • Kata Containers: runs each pod or container in a lightweight VM while keeping the OCI interface.

In Kubernetes, gVisor and Kata can be selected per workload with a RuntimeClass.

Side-by-side comparison

Criterion Virtual machines Containers Serverless
Isolation Strong (own kernel) Medium (shared kernel) Generally strong, managed by the provider
Startup time Slow (OS boot) Fast Fast, but subject to cold starts
Density per host Low High Not applicable (abstracted away)
Overhead High (full OS per VM) Low Invisible, built into the price
OS / kernel Any OS, own kernel Host kernel Provider’s runtimes and limits
Cost model Per running VM The infrastructure running the containers Per use (requests, time, memory)
Operational effort High (OS patching, etc.) Medium (images, orchestration) Low (but with provider lock-in)
Typical use Legacy systems, other OSes, strong isolation Microservices, CI, reproducible environments Events, APIs with variable traffic, short tasks

When to use each

  • VMs: a different operating system, strong isolation between customers, or software that wasn’t built for containers.
  • Containers: reproducibly packaged applications, many per host. If you’re wondering whether you need an orchestrator, see Kubernetes: when to use it or not.
  • Serverless: event-driven workloads, irregular traffic, or teams that don’t want to operate infrastructure. Be wary with high, steady traffic (pay-per-use can cost more than a dedicated machine) or when cold starts are unacceptable.

The models are combined all the time: Kubernetes clusters run on VMs, and a single system can have its API in containers and its file processing in functions.

Security note: containers share the host kernel. A kernel bug or a misconfigured container (running as root, with --privileged, or with the Docker socket mounted) can compromise the entire host. For untrusted code, prefer VMs, microVMs, gVisor, or Kata.

Summary

  • A container is a Linux process with namespaces, cgroups, and a layered file system
  • The OCI specs let images and runtimes from different projects be interchangeable
  • Each VM has its own kernel: more isolation, more overhead
  • Serverless trades control for less operations work and pay-per-use, with cold starts and limits
  • MicroVMs and runtimes like gVisor and Kata sit between containers and VMs
  • Avoid privileged containers and isolate untrusted code more strongly
  • Combine the models according to the workload instead of picking one for everything

A container isn’t a small VM: it’s a process wearing blinders — and knowing that changes how you secure it, size it, and pay for it.

Get updates on new posts and stay in the loop!