Pull up a chair. You have probably typed docker run a few times by now and seen something start in under a second. Compare that with the virtual machine your infrastructure team gave you last year, which took several minutes to come up and wanted its own operating system patches. Both feel like “a separate computer”. They are not the same kind of thing, and once you see the difference a lot of container behaviour stops being mysterious.

Start with what a virtual machine does

A virtual machine pretends to be hardware. A piece of software called a hypervisor (VMware ESXi, KVM, Hyper-V and so on) carves up a physical server and hands each VM a pretend CPU, pretend memory and a pretend disk. On top of that pretend hardware you install a full operating system, with its own kernel. Your application then runs on that guest operating system, which has no idea it is sharing a box with others.

That is a heavy stack. Booting a VM means booting a kernel, starting system services, and so on. The image is usually gigabytes. The benefit is strong isolation: a bug in the guest kernel stays in the guest, and the hypervisor is a comparatively small, well-guarded boundary. You can also run Windows next to Linux on the same server, because each VM brings its own kernel.

Now the container, which is much humbler

A container has no guest kernel. It shares the kernel of the host. What it gets instead is a set of restrictions applied to an ordinary process, using two features built into Linux.

The first is namespaces. A namespace changes what a process can see. With a process-ID namespace, the process thinks it is process number 1 and cannot see other processes on the machine. With a network namespace it gets its own network interfaces and ports. There are also namespaces for mount points (its own view of the filesystem), hostnames, users and others. The process is not in a different computer; it is wearing blinkers.

The second is cgroups (control groups), which change what a process can use. You can say this group of processes may use at most 512 MB of memory and one and a half CPUs. Go over the memory limit and the kernel kills the process, which is the “OOMKilled” status you will meet sooner or later in Kubernetes.

Add a layered filesystem image on top (the “image” is just a stack of read-only file layers plus some metadata) and you have what Docker packages up. Nothing is booting. The kernel simply starts your process with the blinkers on, which is why startup is measured in milliseconds and why you can run dozens of containers on a laptop that would struggle with five VMs.

See it for yourself

Start a container running a long task, then on the host list processes. You will find that container’s process sitting in the host’s normal process list, with an ordinary process ID, next to your browser and your editor. Inside the container it is number 1; outside, it is something like 48213. Same process, two views. A VM would never show you this, because from the host the VM is one big opaque hypervisor process and its guest’s processes are invisible.

Another consequence: a Linux container needs a Linux kernel. When you run Docker Desktop on a Windows or Mac laptop, it quietly starts a small Linux VM and runs your containers inside that. So on your laptop you are using both technologies at once.

What “it works on my machine” has to do with this

The real attraction of containers for developers is packaging, not speed. The image carries your application together with its libraries, the exact Python or Java runtime, and the system packages it expects. The host only needs a compatible kernel and a container runtime. So the thing that passed testing is the same file tree that runs in production. Environment drift, the usual cause of “but it worked in staging”, has far fewer places to hide.

Containers are meant to be disposable. You do not log in and patch one; you build a new image and replace the container. Anything written inside the container’s own filesystem disappears with it, which surprises many freshers the first time their uploaded files vanish after a restart. Data that must survive lives in a mounted volume or an external database.

Where the blinkers slip

Because every container shares one kernel, a kernel vulnerability, or a misconfigured container running with extra privileges, can let a process break out and affect the host and its neighbours. The boundary is thinner than a hypervisor’s. For that reason:

  • Running as a non-root user inside the container is a sensible default, not a nicety.
  • Privileged mode removes much of the protection and should need a good reason.
  • If different customers’ untrusted code must run side by side, many providers put each tenant in its own VM, or use lightweight VMs that behave like containers, rather than rely on namespaces alone.

Resource limits also deserve attention. A container with no memory limit can eat the host’s RAM. A container with a tight CPU limit can look slow even when the machine is idle, because the kernel throttles it once its share is used up.

Which one do you pick?

It is rarely either/or. A typical Indian company’s cloud setup runs containers on top of VMs: the cloud provider gives you virtual machines as worker nodes, and Kubernetes schedules containers onto them. VMs give the hard boundary between your account and the next customer’s; containers give you density and fast, repeatable deployments within your own workload.

Reach for a VM directly when you need a different operating system or kernel, when you run old software that expects to own the machine, or when the isolation requirement is strict. Reach for containers when you deploy many small services, want quick scale-up, and need the same artefact to move from a developer’s laptop to production unchanged. If someone describes a container as a “lightweight VM”, it is a fair first picture, but now you know what is really being lightened: the second operating system is simply gone.

Leave a Reply

Trending