>_ IzzyLab

Hands-on task

This task is step 1 of the course “Containers from the inside: namespaces and cgroups”

A container is just a process

easy~8 min

A new colleague is sure a container is a small virtual machine with its own kernel and that the only way to stop it is through Docker. Show them how it really works.

  1. Start a container named sleeper in the background from the busybox:1.36 image — let sleep run inside it for an hour.
  2. Find the container's main process among the server's ordinary processes and find out its PID on the server.
  3. Stop the container with kill from the server — with a signal to that process. Don't use docker stop, docker kill or docker rm: Docker should be the last to learn the process died.

Don't remove the sleeper container afterwards — from it your colleague will see how it exited.

What you'll practise:
  • docker
  • processes
  • signals
  • pid namespace

Topic breakdown

A container isn't a virtual machine: it has no kernel of its own. It's an ordinary process (or several) on the host, and ps on the host sees it like any other. Kernel mechanisms give it isolation: namespaces decide what the process sees, and cgroups decide how many resources it may take. Because of the PID namespace the process has two numbers: on the host it's, say, 1680, and inside the container it's 1 (both are shown in the NSpid line of /proc/<PID>/status). PID 1 is special to the kernel: it's the init of its namespace, and a signal it hasn't set a handler for is simply not delivered — that's why sleep as PID 1 ignores a plain kill (SIGTERM). The exception is SIGKILL and SIGSTOP sent from the parent namespace: the kernel always delivers those. By convention the exit code of a process killed by a signal is 128 + the signal number: 137 for SIGKILL, 143 for SIGTERM. The parent of the container's process isn't dockerd but containerd-shim: it waits for the process to exit and reports that to Docker. The report isn't instant, so right after kill -9 docker ps may still show Up for a couple of seconds — wait and look again. docker stop does the same as you, just by itself: it sends SIGTERM, waits 10 seconds and finishes the job with SIGKILL.

What's checked

  1. The sleeper container was created during the task
  2. sleeper's main process was ended by a signal
  3. You stopped the process, not Docker

What it looks like