>_ IzzyLab

Hands-on task

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

The nightly export as PID 1

medium~10 min

The nightly export /opt/batch/job.sh is about to move into a container, and there it will become PID 1 — the first process, which sees only itself and its descendants. Before building the image, try it in that setting by hand.

Run /opt/batch/job.sh in a new PID namespace so that:

  • the export is the process with PID 1 in that namespace;
  • ps inside shows only the export and its descendants, not the whole server.

The export must keep running — start it in the background.

What you'll practise:
  • namespaces
  • unshare
  • pid namespace
  • /proc

Topic breakdown

A PID namespace gives processes their own numbering. The first process in a new namespace gets PID 1 and plays the role of init: it adopts orphaned descendants, and when it exits, the kernel kills the whole namespace. From outside the same process has an ordinary PID — both numbers are shown in the NSpid line of /proc/<PID>/status. An important detail: ps and top take the process list from /proc, and /proc shows the PID namespace it was mounted in. So a new PID namespace needs its own mount namespace and a freshly mounted /proc — otherwise the whole server is still visible inside. Docker does both for every container.

What's checked

  1. The export /opt/batch/job.sh is running
  2. The export is PID 1 in its own PID namespace
  3. /proc inside shows the processes of its own namespace

What it looks like