What Docker Actually Starts When You Run a Container


docker run -it --rm ubuntu /bin/bash looks like a request to boot a tiny machine. It is a request to start a process. The distinction explains both Docker’s efficiency and the surprising number of Linux mechanisms needed to make a shell feel as if it has a machine to itself.

The experiment that prompted this tour was a developer building a small container runtime. That is a useful way to inspect Docker: set aside the friendly command for a moment and ask what a runtime must arrange before it can execute /bin/bash.

One kernel, several views

On a Linux host, a container’s application is still an ordinary process scheduled by the host kernel. The host can find it under /proc, assign it a host PID, send it signals, and account for its resource use. Inside, the same process may see itself as PID 1. Both observations can be true because a PID namespace translates the view of process IDs.

This is the first difference from a traditional virtual machine. A VM includes a guest kernel. A Linux container shares the kernel of its Linux host. That does not mean its process sees the same environment as every other host process. Namespaces can give it separate views of process IDs, mounts, network interfaces, hostnames, IPC objects, users, and other resources. Docker’s default isolation is assembled from these pieces rather than from a single kernel feature named “container.”

A container process runs on the shared Linux kernel while namespaces control its view, cgroups account for resources, and a root filesystem supplies its files.
A container is a process with several coordinated boundaries, all enforced by the same Linux kernel.

The pieces do different jobs. A PID namespace changes which processes are visible. A mount namespace changes the mount table. A network namespace can supply a separate network stack and interfaces. A user namespace maps IDs, so UID 0 inside need not be UID 0 outside. Namespaces change the view; they do not impose a memory limit. Control groups, or cgroups, track and constrain groups of processes for resources such as memory and CPU. Network connections also require plumbing beyond creating a namespace: Docker commonly connects an interface pair to a bridge and arranges routing and port publication.

This distinction helps with debugging. If a process sees the wrong files, inspect mounts and the root filesystem. If it can see another process unexpectedly, inspect PID namespace settings. If it exhausts memory, inspect cgroup limits. If it cannot reach a service, inspect its network and port configuration. “Container isolation” is an outcome of several settings, not one switch.

The image becomes a filesystem view

An image is not a running container or a bootable disk. It packages filesystem content and configuration. Docker pulls image layers when needed, makes their files available as a unified root filesystem, and adds a writable layer for a particular container. Several containers can share the read-only image data while each writes to its own top layer. On OverlayFS, lower directories supply image data, an upper directory receives writes, and a merged mount is the view the process sees. Other storage backends implement the same broad idea differently; modern Docker installations may use the containerd image store and snapshotters instead of the older overlay2 storage driver.

The shortcut “images are folders” gets the intuition across but misses the layer machinery. It also risks confusing storage with isolation. A runtime must make the prepared root filesystem appear as / for the new process, typically using a mount namespace and operations such as pivot_root. Merely invoking /some/path/bin/bash from the host would leave absolute paths, mounts, and much of the host environment intact. The Linux pivot_root interface changes the root mount in a mount namespace; it is more than changing the shell’s working directory.

For the example command, Docker resolves and pulls ubuntu if absent, creates a container with its own writable layer and network setup, and starts /bin/bash with an interactive terminal. When the shell exits, the container stops. --rm removes its container state. Data kept only in its writable layer disappears with it; a volume or bind mount is the choice for data that should outlive the container.

Docker run resolves an image, assembles a writable root filesystem, configures Linux isolation and networking, then runc launches the requested process.
The CLI command ends at an ordinary process, after the runtime has prepared its filesystem and boundaries.

Why the runtime has a C stage

Docker’s stack delegates the low-level process setup to an OCI runtime such as runc. The setup has a tricky ordering problem: some namespace operations must happen in the right thread and at the right point before the application starts. Go’s runtime manages multiple OS threads, which complicates thread-sensitive namespace transitions. runc handles its earliest setup in a small C component that runs before the Go runtime initializes, then continues the initialization in Go.

There is another surprise in libcontainer’s own documentation: it executes its current binary as a special init process using /proc/self/exe. The parent and child coordinate while the child receives its isolation and the parent can finish tasks such as cgroup placement and ID mapping. Eventually the child executes the command requested by the user. This re-execution is an implementation technique for safe process setup, not a second container or another kernel.

That implementation detail is why a toy runtime becomes interesting quickly. A small demo can create a namespace and change a root filesystem. A production runtime must also handle process lifecycle, file descriptors, terminal setup, mount propagation, capabilities, security policy, and failures halfway through setup. Each step matters when untrusted code is involved.

Does Docker need root?

Traditional Docker Engine commonly runs its daemon with root privileges. Some kernel and network setup requires privileges, and a rootful daemon has historically been the straightforward way to perform it. That does not mean every container must run as root, nor that root inside a container always has unrestricted host privileges. The daemon’s privilege, the container process’s user, Linux capabilities, and user namespace mappings are separate questions.

Rootless Docker runs both daemon and containers without host root privileges by placing them in a user namespace. It has prerequisites and tradeoffs. Networking is a notable one: ordinary bridge and port setup cannot simply reuse every privileged host operation, so rootless setups use tools such as RootlessKit and slirp4netns or pasta for network and port handling. Calling this “reimplementing the whole network stack” overstates the case; the practical point is that packets need an alternative path when the daemon cannot configure host networking as root.

macOS adds one more layer to the mental model. Linux containers on Docker Desktop for Mac run inside a lightweight Linux VM, because macOS does not provide the Linux kernel interfaces they need. Inside that VM, containers still share one Linux kernel and run as isolated processes. This is why a Mac user can interact with containers without seeing their Linux processes directly in macOS /proc.

A useful model for the next docker run

Think of the command as a sequence: obtain filesystem layers, present them as a root filesystem, arrange namespace views and resource controls, connect networking, then execute the requested program. The container is the resulting process and its managed environment. The image is reusable input. The daemon and runtime are the machinery that prepares and supervises it.

That model makes Docker’s apparent weirdness much more predictable. A process can be PID 1 and still have a different host PID. A file can appear inside the container without existing at the same path on the host. An image can be shared while writes remain private. Rootless execution can work while networking behaves differently. None of these require a special “container” object in the kernel; they follow from how ordinary kernel features are combined.

Sources

100%