Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How Docker containers work: Linux namespaces and cgroups, image layers, the shared kernel, and how containers compare with virtual machines.
Quick answer: A container is an ordinary process running on the host's operating system kernel, with restrictions that make it believe it has a machine to itself. Linux namespaces limit what the process can see: its own process list, network interfaces and file system. Control groups (cgroups) limit what it can use: CPU, memory and disk I/O. A container image packages the application with all of its libraries and files, so it runs the same way everywhere. A virtual machine, by contrast, emulates a whole computer and runs a complete guest operating system. Containers share the host kernel, which makes them start in milliseconds and use far less memory, at the cost of weaker isolation.
Also Read: Blue-Green vs Canary Deployments: Reducing Risk - Tooling
"It works on my machine" is one of the oldest complaints in software. An application depends on a particular language version, system libraries, configuration files and environment variables. A slight difference between a developer's laptop and the production server causes failures that are painful to diagnose.
Containers fix this by shipping the application together with its entire environment. If it runs in the container on your laptop, the same container runs on the server.
There is no "container" object in the Linux kernel. A container is a normal process (see what happens when you run a program) started with three kernel features applied to it. The Docker overview describes the same building blocks.
A namespace gives a process its own private view of one part of the system.
| Namespace | Isolates | Effect inside the container |
|---|---|---|
| PID | Process IDs | Its main process is PID 1 and it cannot see the host's processes |
| Network | Interfaces, IP addresses, ports, routes | It has its own network stack and can listen on port 80 without conflict |
| Mount | The file system tree | It sees only its own files |
| UTS | Hostname | It has its own hostname |
| IPC | Shared memory and message queues | It cannot interfere with other processes' IPC |
| User | User and group IDs | Root inside can be an unprivileged user outside |
Cgroups set limits and track usage:
Without them, one misbehaving container could starve every other one on the host.
The container's root directory is switched to the image's file system. The process sees a complete Linux directory tree, with /bin, /lib and /etc, that belongs to the image, not to the host.
Additional restrictions tighten security: dropped capabilities, seccomp filters that block risky system calls, and mandatory access control profiles.
A container image is a read-only template: a file system snapshot plus metadata such as which command to run.
Images are built from a Dockerfile:
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Each instruction creates a layer: a set of file changes on top of the previous one. Layers are identified by a hash of their content. This design pays off in several ways:
When a container starts, a union file system stacks the read-only layers and adds a thin writable layer on top. Changes made inside the container go there, using copy-on-write. Delete the container and that layer is gone.
Layers never shrink an image retroactively: a file added in one layer and deleted in the next still exists in the first. Secrets copied into an image stay recoverable for the same reason.
| Virtual machine | Container | |
|---|---|---|
| What is virtualised | Hardware | The operating system |
| Kernel | Each VM runs its own | Shared with the host |
| Includes | A full guest OS | Only the app and its libraries |
| Start time | Seconds to minutes | Milliseconds to seconds |
| Size | Gigabytes | Megabytes |
| Memory overhead | A whole OS per VM | Little beyond the app itself |
| Isolation | Strong: a hardware boundary enforced by the hypervisor | Weaker: a shared kernel |
| Density per host | Tens | Hundreds or thousands |
| Can run a different OS | Yes | No: Linux containers need a Linux kernel |
A hypervisor runs virtual machines, each with a full operating system that believes it is on real hardware. A container runtime asks the one host kernel to start isolated processes.
They are complementary. In the cloud, containers almost always run inside virtual machines: VMs separate customers, and containers pack applications efficiently within them.
When Docker runs on macOS or Windows, it quietly runs a small Linux virtual machine, because Linux containers need a Linux kernel.
Because all containers on a host use the same kernel, a kernel vulnerability, or a container given too many privileges, can allow an escape to the host. Sensible practice:
For workloads that need stronger separation, sandboxed runtimes add a layer: gVisor intercepts system calls in user space, and Kata Containers and Firecracker wrap each container in a lightweight virtual machine.
Docker made containers popular in 2013 by giving them a simple tool and image format. The underlying kernel features already existed.
Today the formats are open standards under the Open Container Initiative (OCI): an image built with one tool runs under any compliant runtime. The pieces:
| Component | Role |
|---|---|
| Docker CLI and daemon | Build, run and manage containers on one machine |
| containerd | The runtime that manages container lifecycles |
| runc | The low-level tool that sets up namespaces and cgroups and starts the process |
| Registry (Docker Hub and others) | Stores and distributes images |
| Podman, Buildah | Alternative tools without a central daemon |
Running containers across many machines needs an orchestrator; see what Kubernetes does. Building images automatically is usually part of a CI/CD pipeline.
A virtual machine emulates hardware and runs a complete operating system. A container is an isolated process sharing the host's kernel, which makes it far lighter and faster to start.
An image is a read-only template. A container is a running instance of an image, with a writable layer on top.
They provide useful isolation, but weaker than virtual machines, because the kernel is shared. Run them with minimal privileges and keep hosts patched.
No. Docker is one set of tools for building and running containers. The technology underneath is part of the Linux kernel, and other tools use it too.
A container is not a small virtual machine. It is an ordinary process that has been given a restricted view of the system and a budget of resources, started from an image that carries everything it needs. That simplicity is why containers start instantly and pack densely, and the shared kernel is why they need care when isolation really matters.