Docker: Why Containers Aren't Tiny Virtual Machines

Outline

Transcript

0:00 An app inside a Docker container can report process one while the Linux host lists another ID. We'll follow that confusing result back to one running application. Linux can give a process its own view of process IDs. That's a PID namespace. The application still runs through a shared Linux kernel. We're following Docker's official docs, the OCI spec for configuring the process, and the runc code that starts it. The source links are in the description. For example, we started a Python child in a new namespace, without running Docker or runc.

0:31 Python reported one. An outside observer read that child's process record. Both numbers belong to that child. Read across its row: the host's PID comes first. The last number matches what Python reported. And on Docker Desktop, that shared Linux kernel belongs to its Linux virtual machine. The Linux containers aren't making system calls into macOS. So our Linux host can itself be a VM. Within that Linux system, who builds the application's view: Docker or the application? Follow that setup path from the Docker command to runc.

1:02 That's the program that prepares the process environment before starting the application. The OCI specification describes that environment's configuration. Our small entry requests a new PID namespace. An entry with a namespace path would join one that's already there. Watch the runc process that makes the request. Hold on. My first thought was that the request renumbers the running process. Does the caller suddenly become process one? No. The first source line asks for the view with unshare. The next excerpt shows it creating another child.

1:34 That child gets the new numbering; the caller keeps its original view. Ah, we haven't changed the caller out from under itself. But that child is still runtime code. How does it become our application? Once filesystem and security setup finish, it calls exec with the application and its arguments. Exec replaces the program while retaining that process's identity. I see. The application takes over that prepared process. We never booted another kernel. But that process sees familiar files. Suppose the image has Ubuntu directories, an application and libraries.

2:09 So where's the Ubuntu kernel in that tree? The image supplies userspace: the application and files it uses above the kernel. Running it under runc doesn't boot an Ubuntu kernel from those directories. Runc prepares mounts and switches the process's filesystem root. Follow the child's/arrow to the prepared image directory. That's where an absolute path starts. The observer's/stays where it was. Have the child and the observer open the settings file on screen. Same path, two files. But what does a mount change?

2:41 It attaches a filesystem at a directory. Our illustrated data filesystem goes at/data. Its files appear there; neither process's/has to move. A mount namespace keeps that attachment list separate. Moving/instead changes where a path starts. And both still use the Linux kernel below. That's why a library problem can lead to the image, while an unsupported kernel feature won't appear just because those packaged files changed. Those filesystem views leave a separate network question. A network namespace separates interfaces and sockets.

3:14 That distinction can explain a failed database connection. Suppose we're debugging a service that calls localhost expecting the host database. Its connection fails, even though that database is healthy. Ah, both sides say localhost. Where does the request actually go? It stays on the service's loopback. The database is listening in the host's stack. So a container's label doesn't tell me which views were separated. What if that PID entry is missing altogether? Omit the PID namespace entry and the process inherits the runtime's PID view.

3:47 No new view means it doesn't receive a fresh process-one role. Then two applications can share that namespace if both inherit the same runtime view. Now suppose the app has its own PID view and a 256 mebibyte memory limit. The Linux host has memory free. Where would you look for that limit? I'd check the resource controls. Linux control groups, usually called cgroups, organize processes for resource control. A configured memory controller can impose that limit. So changing that setting wouldn't renumber the application.

4:19 The limit is an illustration; we didn't apply it in our experiment. Right. Even with that cap, it's still the Linux host's memory. And those controlled processes can have children. Imagine this application spawning a worker and helper. Can a worker keep running when its parent exits? If that parent is the PID namespace's init, no. That's its initial process. When init exits, the kernel terminates the other members in that view. Oof. So a startup script could launch the app in the background, exit successfully, and still take that app down?

4:52 That depends. Is that script the namespace's init, or is another process holding PID one? Yeah, that role decides it. Our illustrated script holds PID one. That clean exit takes its own app down. It can exec the app to hand over that role, or an init program can manage the app as its child. Check who is playing that init role. So that process tree still needs a Linux kernel. On the Linux server, both arrows end at the host kernel. Now follow the same two containers on the Mac. They meet inside Docker Desktop's Linux VM.

5:27 Both containers use the kernel that VM booted. There are other runtime choices too, so this runc explanation isn't every possible container boundary. Even here, namespaces alone aren't a complete security guarantee. I think I'd check what that process is allowed to do before starting an unfamiliar image. The Python record paired our child's two IDs. The documented source trace showed how runc prepares an application without booting another kernel. Thanks for listening to Learning Podcasts.