Images and containers

Why does software that works on your machine fail on the server?

Installed on each machineYour laptopPython 3.12the libraries you added✓the app runsThe serverPython 3.8other library versions✗the same app failsPacked with the appImageyour appPython 3.12its librariesContaineron your laptopContaineron the serverrundocker run nginxfetch the image nginx and start a containerAn image packs a program with everything it needs. A container is one running copy of an image.A program depends on what is installed around it: the language version, libraries, system files. Move it to a machinewhere those differ and it breaks. An image carries those files along, so the laptop, the test server and productionall run the same bytes. The image is files on disk; a container is what you get by running it, and one image canstart many containers. A container is not a small virtual machine, as the next page shows.

An image packs a program with everything it needs; a container is a running copy of an image, the same on every machine.

Containers and VMs

Containers share one kernel; each VM brings its own

Three virtual machinesAppAppAppGuest OS, kernelGuest OS, kernelGuest OS, kernelHypervisorHardwarestarts in about a minuteeach carries a whole operating system: gigabytesseparate kernels: strong isolationThree containersAppAppAppContainer runtimeHost OS: one kernel for allHardwarestarts in under a secondeach carries the app and its libraries: megabytesone shared kernel: weaker isolationContainers share the host's kernel. Virtual machines each bring their own.That is why containers are small and start at once, and also why a Linux container needs a Linuxkernel underneath. The shared kernel is a thinner wall than a hypervisor, so clouds run customers'containers inside virtual machines: both techniques, one inside the other.

A VM carries an operating system. A container carries only the app.

What a container is

A container is a process that is shown a smaller world

Seen from the hostSeen from inside the containerIts process id482111Processes it can see3122Hostnameserver-073f2a9c1d77b4Root directory /the server's diskthe image's filesNetwork address10.0.0.7172.17.0.2A container is an ordinary process that has been shown a smaller world.The web server here is one process on the host, like any other. The kernel simply answers itsquestions differently: which processes exist, what the machine is called, what is on the disk. Thereis no container "thing" in Linux, only a process with walls around what it can see.

One process, two views: the host sees everything, the container sees only itself.

What a container is · Going deeper · 1 of 4

Lab: one process, seen from both sides

TypeLook fordocker run -d --name web nginxdocker exec web hostnamedocker exec web cat /proc/1/commdocker top webdocker rm -f weba long id: the container is runninga short id, not the name of your machinenginx: inside, the server is process 1the same processes with the host's ids, in the thousandsthe container and its process are goneLab: look at one process from both sides.You need Docker or Podman. On Linux, "ps aux | grep nginx" on the host shows the very same process.On macOS and Windows the host is a small Linux virtual machine that Docker runs for you.

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

NamespaceWalls offSo inside the container you seepidprocess idsonly your own processes, numbered from 1mntthe file systemthe image's files, mounted as /netnetwork devices, addresses, portsa private eth0, and port 80 all to yourselfutsthe hostnamea hostname of your ownipcshared memory and queuesnothing of any other container'suseruser and group idsroot inside can be an ordinary user outsideEach namespace is one wall: it limits one kind of thing that a process can see.A container runtime creates a fresh set of namespaces and starts your program inside them. Twocontainers can both listen on port 80 and both have a process 1, because each has its own net andpid namespace. Containers in one Kubernetes pod share a net namespace, which is why they share an address.

pid, mnt, net, uts, ipc, user: six walls, six private views.

What a container is · Going deeper · 3 of 4

Namespaces limit what it sees; cgroups limit what it uses

The host: 16 GB of memorythis container may use 256 MB of itEnlarged: the 256 MB limitusing 180 MB: fineasks for 300 MB: killed, exit code 137docker run --memory 256m --cpus 0.5 nginxNamespaces limit what a process can see. Control groups limit what it can use.A control group, or cgroup, gives a process a budget of memory, processor time and disk activity.Go over the memory limit and the kernel kills the process. Go over the processor limit and it is onlyslowed down. Without limits, one greedy container can starve every other on the machine.

A cgroup is a budget. Over the memory limit, the process is killed.

Stopping and exit codes · Going deeper · 4 of 4

docker stop asks, waits 10 seconds, then kills: the exit code tells which

docker stop webSIGTERM: please finishSIGKILL: no appealhandles the signal: exits in a second, code 0ignores it: killed at 10 s, code 1370 s10 s: the default wait (-t changes it)Exit codeWhat ended the container0the program finished by itself1the program reported an error of its own125docker run itself failed, for example a bad flag126the command was found but could not be run127the command was not found in the image143128 + 15: ended by SIGTERM, which it did not handle137128 + 9: ended by SIGKILL, or killed for memoryCMD python app.pyshell form: /bin/sh is process 1, and it does not pass the signal onCMD ["python", "app.py"]exec form: your program is process 1, and receives the signalA stop is a request, a wait, then a kill. A program that never hears the request loses its clean shutdown.docker stop sends SIGTERM to process 1 of the container, and SIGKILL once the wait is over. Kubernetes does the same when itremoves a pod, with a default wait of 30 seconds (terminationGracePeriodSeconds). The kernel gives process 1 no defaulthandling of signals, so the program must handle SIGTERM itself, or run under a small init: docker run --init.

SIGTERM first, SIGKILL after the wait. 137 is 128 + 9.

The standard

An open standard: any tool builds, any runtime runs

docker, podmanwhat you typecontainerd, CRI-Omanages containersruncbuilds the wallsLinux kernelruns the processkubeletKubernetes, on each nodeCRI✓OCI image specification: what an image is✓OCI runtime specification: how to run oneAn open standard means any tool can build an image and any runtime can run it.Docker made containers popular, and its formats became the Open Container Initiative standards. Thework is done in layers: a command-line tool, a daemon that manages containers, and a small runtimethat asks the kernel for the walls. Kubernetes talks to the daemon directly and needs no Docker.

A tool, a daemon, a runtime, the kernel. All speak OCI.

crictl · Going deeper

On a Kubernetes node there is no docker: crictl asks the runtime

The jobWith Docker, on your laptopOn a Kubernetes nodeList running containersdocker pscrictl psList pods(Docker has no pods)crictl podsList imagesdocker imagescrictl imagesRead a container's outputdocker logs IDcrictl logs IDRun a command insidedocker exec -it ID shcrictl exec -it ID shInspect one containerdocker inspect IDcrictl inspect IDPull an imagedocker pull nginx:1.27crictl pull nginx:1.27A Kubernetes node runs containerd or CRI-O, not Docker. crictl is the tool that talks to them.Kubernetes removed its built-in adapter for Docker (dockershim) in v1.24. The kubelet calls the runtime through the ContainerRuntime Interface, and crictl uses the same interface, so it sees exactly what the kubelet sees, pods included. Images builtwith Docker still run, because they are OCI images. crictl matters most when the API server is down and kubectl cannot answer.

The kubelet speaks CRI to containerd. crictl speaks it too.

Lesson complete

You finished “Why containers, and running your first one”

Nicely done.

Open in a new tab