Images and containers
Why does software that works on your machine fail on the server?
An image packs a program with everything it needs; a container is a running copy of an image, the same on every machine.
It works on my machine. Every engineer has said it. The app runs on your laptop because of what is installed there: this version of Python, these libraries. The server has other versions, and the same code fails. The fix is to stop depending on the machine. Pack the app together with the exact Python and libraries it needs into one file set, called an image. Running an image gives you a container. Run it on the laptop, run it on the server: same files inside, same behaviour. docker run with the name of an image is the whole command.
Containers and VMs
Containers share one kernel; each VM brings its own
A VM carries an operating system. A container carries only the app.
People often call a container a lightweight virtual machine. The comparison shows why that is not quite right. A virtual machine pretends to be hardware. Each one boots a complete operating system with its own kernel, which costs gigabytes and about a minute. Containers skip all that. They are processes on one shared kernel, so they hold only the application, weigh megabytes and start in a moment. The price is a thinner wall between neighbours.
What a container is
A container is a process that is shown a smaller world
One process, two views: the host sees everything, the container sees only itself.
A container sounds like a box. It is really a process. Take a web server running in one, and ask the same questions from two sides. Its process id: forty eight thousand on the host, but number one from the inside. How many processes exist? Hundreds outside. Two inside. The hostname is different. So is the root of the file system: inside, there is only what the image brought. And it has a network address of its own.
What a container is · Going deeper · 1 of 4
Lab: one process, seen from both sides
Start a container, then look at it from inside and from the host.
What a container is · Going deeper · 2 of 4
Each namespace is a wall around one kind of thing
pid, mnt, net, uts, ipc, user: six walls, six private views.
The walls are a kernel feature called namespaces, and there is one for each kind of thing a process can see. The pid namespace hides other processes. The mount namespace swaps the file system for the one in the image. The network namespace gives the process private network devices and ports. The U T S namespace gives it a hostname. And two more wall off shared memory and user accounts, so that root inside a container need not be root on the machine.
What a container is · Going deeper · 3 of 4
Namespaces limit what it sees; cgroups limit what it uses
A cgroup is a budget. Over the memory limit, the process is killed.
Walls alone are not enough. A process that cannot see its neighbours can still eat all their memory. So the kernel also gives it a budget. This host has sixteen gigabytes, and the container is allowed the thin purple slice: two hundred and fifty six megabytes. Enlarged, that is the grey bar. Using one hundred and eighty is fine. If the process asks for three hundred, it crosses the line, and the kernel kills it.
Stopping and exit codes · Going deeper · 4 of 4
docker stop asks, waits 10 seconds, then kills: the exit code tells which
SIGTERM first, SIGKILL after the wait. 137 is 128 + 9.
The standard
An open standard: any tool builds, any runtime runs
A tool, a daemon, a runtime, the kernel. All speak OCI.
Starting a container passes through four hands. You type a command into a tool such as Docker or Podman. It asks a daemon, usually containerd, which fetches the image and keeps track of containers. The daemon calls a small program named runc, which asks the kernel for the namespaces and control groups. The kernel starts your process inside them. Kubernetes enters this chain at the second link. Its agent on each node talks straight to containerd.
crictl · Going deeper
On a Kubernetes node there is no docker: crictl asks the runtime
The kubelet speaks CRI to containerd. crictl speaks it too.
Lesson complete
You finished “Why containers, and running your first one”
Nicely done.