The page you are reading right now is being served out of a container. So is the database behind it, and a dozen other things on the same host, all of which believe they have the machine to themselves. None of them is a virtual machine, and the gap between what people think is happening there and what is actually happening is the reason this series starts here rather than with a docker run command.
Almost everyone arrives at Docker with the same mental model: a container is a very small, very fast virtual machine. It is an understandable guess. It is also wrong in a way that makes everything afterwards confusing — why containers start in milliseconds, why they are measured in megabytes, why they cannot run a different operating system, and why “the data disappeared” is the single most common first disaster.
A separate house, or a locked room
A virtual machine virtualises hardware. The hypervisor fabricates a pretend CPU, pretend disks and a pretend network card, and on top of that you install a complete operating system with its own kernel. It is a separate house: its own foundations, its own plumbing, its own front door. That is why it takes a minute to boot and gigabytes to store — you are running a second computer.
A container virtualises nothing of the sort. There is no second kernel. The processes inside a container are ordinary processes running on the host’s kernel, which have simply been fenced off so that they cannot see or touch anything they have not been given. It is a locked room in a building you are already standing in — sharing the foundations, the plumbing and the power, with a door that only opens one way.
| Virtual machine | Container | |
|---|---|---|
| What is virtualised | The hardware | Nothing — only the view a process gets |
| Kernel | Its own, a full guest OS | The host’s, shared |
| Starts in | Tens of seconds | Milliseconds |
| Costs you | Gigabytes | Megabytes |
| Can run | Any OS, including Windows on Linux | Only what the host kernel can run |
| Isolation | Very strong — a hardware boundary | Good, but a kernel bug is a shared bug |
That last row is the honest trade. Containers are lighter precisely because they share the kernel, and sharing the kernel means the isolation is enforced by software rather than by a hardware boundary. For nearly everything that is a fine bargain. For running genuinely untrusted code from strangers, a VM is still the stronger wall.
Proving it: there is no container
Here is the part that makes it click. Start a container, then look at the host’s process list — not inside anything, just the plain machine:
$ docker run -d --name web nginx
d7f3a1c9e2b4...
$ ps -eo pid,comm | grep nginx
14823 nginx # just a process. On the host. In the normal process list.
$ sudo lsns -p 14823
NS TYPE NPROCS PID COMMAND
4026532611 mnt 2 14823 nginx: master process
4026532612 uts 2 14823 nginx: master process
4026532613 ipc 2 14823 nginx: master process
4026532614 pid 2 14823 nginx: master process
4026532616 net 2 14823 nginx: master process
There it is, sitting in the host’s process table alongside everything else. You could kill it from the host. The only thing that makes it “contained” is the second command’s output: a set of namespaces it has been placed in.
There is no “container” object
Search the Linux kernel for a container data structure and you will not find one. A container is not a thing the kernel knows about — it is a convention, assembled by Docker out of several kernel features that all existed independently beforehand. Namespaces, cgroups and a layered filesystem, stacked together and given a friendly name. Once you know that, the behaviour stops being magic and starts being predictable, which is the entire point of this post.
Namespaces: what a process can see
A namespace limits a process’s view of the system. Same kernel, same machine — but the process is shown a deliberately partial picture and has no way to look past it.
| Namespace | What it isolates |
|---|---|
pid | Process IDs. Inside, your app is PID 1 and simply cannot see the host’s processes. |
mnt | Mount points — its own view of the filesystem tree, which is why / inside is not / outside. |
net | The whole network stack: its own interfaces, routes and ports. |
uts | Hostname. |
ipc | Shared memory and semaphores. |
user | UID and GID mapping — root inside can be an unprivileged nobody outside. |
The mnt namespace is the one that pays back the filesystem post directly. Everything from that post is still true — there is one tree starting at / — it is just that the container has been handed a different tree and told it is the only one. And the net namespace is why a container can listen on port 80 while the host is also listening on port 80, with no argument between them: those are two different network stacks.
cgroups: what a process can use
Namespaces handle seeing. Control groups handle consuming — how much CPU, memory, disk and process count a group is permitted. Without them a container could happily eat the whole machine, because remember: it is just a process, and processes are greedy.
$ docker run -d --name web --memory=512m --cpus=1.5 nginx
Exceed the memory limit and the kernel steps in. Linux has no polite way to ask a process to use less memory than it has already asked for, so when a limit is hit it reaches for the OOM killer — out-of-memory killer, the kernel routine that picks a process and terminates it outright to claw memory back. With a cgroup limit in place the victim is chosen from inside that container, so the container dies and only that container; the rest of the host carries on. Without one, the kernel picks from the whole machine, and what it decides to kill may well be something you cared about. On a box running other people’s workloads, that limit is not a nicety.
Images and containers are not the same noun
The other thing newcomers blur together. An image is a read-only template — a stack of filesystem layers, built once, never modified. A container is a running instance of one. You can start twenty containers from a single image and they all share the exact same layers on disk, because nothing is writing to them.
| Image | Container |
|---|---|
| Read-only, built from a Dockerfile | A running process using that image |
| Stored once, shared by many | Has its own thin writable layer |
docker images | docker ps |
Deleted with docker rmi | Deleted with docker rm |
Each instruction in a Dockerfile produces one layer, and layers are content-addressed and shared. That is why pulling your second image based on ubuntu:24.04 is nearly instant — the base is already on disk, and only the new layers come down. It is also why the order of a Dockerfile matters more than it looks: put the slow, rarely-changing steps first and the fast, frequently-changing ones last. Copy your source code in before you install your dependencies and every one-character change throws away the cached install and does it all again.
Which brings us to the thing that bites everyone exactly once:
Where the data went
A running container gets a thin writable layer stacked on top of the image’s read-only ones. Every file it creates or changes lands there — logs, uploads, that database you just spent an afternoon populating. And that layer belongs to the container, not the image. Remove the container and it goes with it.
This is not a bug, it is the design: containers are meant to be disposable, which is exactly what makes them easy to rebuild and redeploy. But it means anything that must outlive the container has to be deliberately stored outside it, in a volume:
$ docker volume create pgdata
$ docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres
$ docker rm -f db # the container is gone
$ docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres
# ...and the data is still there
The first disaster, more or less universally
Someone runs a database in a container without a volume, uses it happily for a fortnight, then rebuilds the container to change a setting — and every row is gone. There is no recovery. The writable layer was deleted along with the container and nothing else ever had a copy.
Worth internalising early: docker rm is not systemctl restart. Removing a container destroys state by design. Ask “where does this write, and is that somewhere that survives?” before the thing is holding anything you care about — and remember that a volume is not a backup either. It is on the same disk on the same host.
Getting to it: ports
Because of that net namespace, a container’s ports are its own and reach nobody by default. To make one reachable you publish it, mapping a host port to a container port — host first, container second:
$ docker run -d -p 8080:80 nginx # host 8080 -> container 80
$ docker run -d -p 127.0.0.1:8080:80 nginx # ...but only reachable from this machine
Containers on the same user-defined network also reach each other by name — your app connects to db:5432 rather than to an address, because Docker runs a small DNS resolver for them. Everything from the networking series still applies; it is just happening inside a namespace.
Worth knowing now, explained properly later
Notice the second command above. Publishing a port can reach further than you expect — far enough that the firewall you configured may not be consulted at all, and ufw status can report “deny” while the container is perfectly reachable from the internet. It is a genuine trap and it deserves more than a footnote, so it gets its own post once we have covered how Docker networking is actually wired.
The one-sentence version
A container is not a small virtual machine: it is an ordinary process on the host’s own kernel, fenced off by namespaces (limiting what it can see) and cgroups (limiting what it can use), running on a stack of read-only image layers with one thin writable layer on top — and that writable layer, along with everything in it, is deleted when the container is, which is why anything you intend to keep belongs in a volume.
That is the mental model. With it in place the commands stop being incantations and start being obvious, which is just as well, because next we write the recipe: Dockerfiles — how an image is actually built, layer by layer, and how to build one that is small, fast to rebuild and not quietly running everything as root.
