Build a container without an engine: verified Alpine rootfs, unshare, lsns, nsenter and pivot_root, tested on Debian 13 to see what a container really is.
The issue opens pre-filled with this page title and URL.
The previous lesson established that a container is a process to which the Linux
kernel gives new namespaces. Here you build one by hand with unshare, then you
find it again from the host (the machine that runs it), you enter it with
nsenter and you stop it. This container will have only namespaces and a root
filesystem of its own. The cgroups, which limit its consumption, come in Limit a
container.
You will also see why chroot alone is not enough and what pivot_root changes.
No engine is involved. The outputs were tested on a Debian 13 virtual machine
(kernel 6.12, util-linux 2.41.5).
What you will learn
Explain what the root filesystem (rootfs) of a container is and what it changes for the process.
Prepare an Alpine rootfs whose archive is checked with a SHA-256 sum.
Start an isolated process with unshare and inspect it from the host.
Enter this container with nsenter, without the root trap.
Tell chroot from pivot_root and list what is still missing.
Prerequisites
Comfortable with the Linux command line and the notion of a process (PID, signal).
root or sudo access on a disposable virtual machine, never on your workstation: the commands mount filesystems and kill processes. The rootfs section opens a root shell with sudo -i.
Having read the previous lesson, Containers or virtual machines: which one to choose?: it reads the namespaces of an ordinary process in /proc and concludes that a container is a process to which the kernel gives new ones. This lesson builds one.
No Docker: this lesson is part of level 1 of the Docker course, before Docker is installed. Only the kernel's tools and util-linux are used here.
The Linux guide Understanding Linux namespaces
details the eight types of namespaces, the user namespace and ip netns. This
lesson takes from it only what is needed to assemble a complete container.
What is a container, concretely?
The container of this lesson is a process of the host, as the previous lesson
established. It combines two ingredients of the kernel: namespaces (the
namespaces read in /proc), which limit what it sees (processes, host name,
mounts, network), and a root filesystem of its own, which changes its /. It
does not have a cgroup of its own yet, so it has no consumption limit. There is no
second kernel and no emulated hardware: the container calls the same kernel as
every other process of the machine, and it starts without booting a system. Its
root filesystem (a rootfs, introduced in the next section) weighs only 8.7 MB,
because it contains no kernel, whereas a virtual machine carries a complete
system.
The diagram shows the machine as the kernel sees it. Look first at the containerized
process at the top: five namespaces (pid, uts, mnt, net, ipc) give it its
own view. The three below, user, cgroup and time, remain those of the
host: unshare does not create them here. Everything rests on the single kernel,
at the bottom.
A single kernel for all processes: only five namespaces are created for the container of this lesson.Source · SVG
The table sums up the five unshare options used below and what each one isolates.
They each correspond to a type of namespace described in detail in the Linux guide.
unshare option
Namespace
What the process sees of its own
--pid (with --fork)
pid
its numbering: it is PID 1
--uts
uts
its machine name (hostname)
--mount
mnt
its table of mounts
--ipc
ipc
its message queues and shared memory
--net
net
its network interfaces (here, lo only)
Swipe to see more
How do you prepare a verified Alpine rootfs?
The commands of this lesson are written without sudo. So first open a root
shell: the processes you create must belong to root so that lsns -p and
readlink /proc/$PID/ns/* can see them. The unshare, mount and mknod
commands require these rights anyway.
Terminal window
sudo-i
id-un
root
The prompt must end with # and id -un must answer root. Keep this shell for
the whole lesson.
A rootfs (root filesystem) is a tree of files that serves as the / of the
container: its binaries, its libraries, its /etc. You use here the Alpine Linux
3.24.2 minirootfs, an archive of a few megabytes designed for this use. Check it
before extracting it: a SHA-256 sum is the fingerprint of a file, and if a single
byte changes, it changes. This is not yet an OCI image (the standard format that
packages the file layers of a container and its metadata, introduced in the lesson
Read an OCI image and check its digest), just an archive of files.
The check must print alpine-minirootfs-3.24.2-x86_64.tar.gz: OK, and du -sh
about 8.7M /srv/alpine. If the line says FAILED, do not go on: the archive
is corrupted or tampered with. The extracted content looks like the root of a
minimal Linux system:
Répertoiresrv/
Répertoirealpine/
Répertoirebin/
…
Répertoiredev/
…
Répertoireetc/
…
Répertoirehome/
…
Répertoirelib/
…
Répertoiremedia/
…
Répertoiremnt/
…
Répertoireopt/
…
Répertoireproc/
…
Répertoireroot/
…
Répertoirerun/
…
Répertoiresbin/
…
Répertoiresrv/
…
Répertoiresys/
…
Répertoiretmp/
…
Répertoireusr/
…
Répertoirevar/
…
The file /srv/alpine/etc/alpine-release contains 3.24.2: this directory really
is another system than Debian.
Why is chroot not enough?
chroot changes only the process's root: it sees another /, but everything
else is shared with the host. To check this, first note your machine's
hostname, mount proc in the rootfs (ps reads the /proc directory), then start
a shell rooted in /srv/alpine.
The os-release file does announce Alpine, but the hostname is that of the host
(here conteneurs-fonda-lab, yours will be different), and ps prints more than a
hundred lines, header included (113 on the tested VM; the number varies from one
machine to another), among them systemd as PID 1 and the kernel's processes. The
shell sees other files, but the same processes and the same machine name. A
chroottherefore does not make a container: it isolates neither the
processes, nor the machine name, nor the network.
How do you isolate a process with unshare?
unshare starts a program in new namespaces, and --root gives it the
container's root. A single command builds the container of this lesson:
The table above describes --uts, --ipc and --net. Three other points need an
explanation. --pid creates a process namespace, but it applies only to the
children of the calling process: --fork therefore makes unshare create a
child, and that child becomes PID 1. --mount-proc mounts /proc again in the new
mount namespace (--mount); without it, the rootfs's /proc would stay empty
and ps would print nothing. --root changes the root. Here is the output:
hostname: conteneur-main
--- ps
PID USER COMMAND
1 root sh
4 root ps
--- ip link
1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN qlen 1000
Four things can be read in this output. The shell is PID 1, and the list contains
only sh and ps. The name conteneur-main did not touch the host, which keeps
conteneurs-fonda-lab. A single interface exists, lo, in the DOWN state: no
network is configured. Finally the process is still root (uid 0): isolation
removed no privilege.
How do you see this container from the host?
Seen from the host, the container is a process like any other, with a normal
PID. You start it in the background with a sleep so that you have time to
observe it, then you query the kernel with lsns and /proc.
pgrep -n -x sleep returns the number of the most recent process whose name is
exactly sleep, so the container's as long as no other sleep is started. exec
replaces the container's shell with sleep, which thus becomes its first process.
On the tested VM, sleep carries PID 4966 on the host side, while it believes it is
PID 1 inside. Your number will be different. lsns -p lists its namespaces
(excerpt: the COMMAND column is shortened; the PIDs, the namespace numbers and
the counters change from one launch to another):
Look at the NPROCS column: the first three lines (time, cgroup, user) count
111 processes and point to the host's PID 1. These three namespaces are shared
with the host: unshare did not create them. The other five are specific to
the container. The four that count 2 processes (mnt, uts, ipc, net)
contain the unshare process, which placed itself inside before starting its
child, and sleep. The pid namespace counts only one, sleep: unshare stays
in the host's pid namespace, because --pid applies only to its children. Each
namespace is also a symbolic link in /proc/<PID>/ns/, which allows a direct
comparison.
For pid, uts, mnt, net and ipc, the two inode numbers (the namespace's
identifier) differ (for example pid:[4026532742] against pid:[4026531836]).
For user and cgroup, they are identical: user:[4026531837] and
cgroup:[4026531835]. Finally ls -l /proc/$PID/root prints
/proc/4966/root -> /srv/alpine: the host sees the container's root as a simple
link.
How do you enter a container with nsenter?
nsenter joins the namespaces of an existing process, but it does not change the
root by itself. If you forget this, you believe you are in the container while you
are reading the host's files. Here is the trap, tested:
cat: /etc/alpine-release: No such file or directory
The hostname is indeed the container's (uts namespace joined), but the shell
keeps the host's root: ps reads the host's /proc (hence systemd as PID 1)
and /etc/alpine-release does not exist. The --root and --wd options fix this
by aligning the root and the working directory with those of the target process:
The output this time shows sleep as PID 1, the shell sh and ps in the list,
then 3.24.2: you are really inside. Now ask the container for its kernel
version, then the host:
Terminal window
nsenter--target"$PID"--all--root--wduname-r
uname-r
6.12.118+deb13-amd64
6.12.118+deb13-amd64
Both commands must print the same number: that of the host's kernel, which uname -srm showed in the previous lesson. The container has an Alpine root, but no
Alpine kernel. You can also enter a single namespace: nsenter --target "$PID" --uts hostname answers conteneur-main without changing anything else. docker exec and kubectl exec rest on the same principle (joining the namespaces of an
existing process), with security steps added. The guide Understanding Linux
namespaces details other uses of
nsenter.
Bring up the container's lo interface from the hostExercise 1 of 2 · ~8 min · terminal
The container's lo interface is in the DOWN state: even 127.0.0.1 does not answer. You want to bring it up from the host, without opening a shell in the container, by joining a single namespace.
Check that the network does not answer
Start the container of the section "How do you see this container from the host?" in the background again with exec sleep 600, store the number of the sleep in PID, then enter it with nsenter --all --root --wd to send a single ping to 127.0.0.1 (the -c option sets the number of packets). Note the error.
Bring lo up by joining only the network
From the host, run the host's ip command in the container's network namespace only, to set lo to the up state (ip link set <interface> up), then read the list of interfaces in that namespace again with ip -br link (-br gives one short line per interface). Note the state of lo.
Check from the inside
Run the ping of the first step again, with two packets this time, and read the loss percentage. Then compare with the same ip -br link command run on the host without nsenter, and stop the container with kill -KILL (the next section, "Why does kill -TERM not kill the container's PID 1?", explains this choice).
unshare
nsenter
ip
ping
A hint 1 / 1
The nsenter option that joins the network namespace is named after the namespace, and without --root the command that runs is the host's.
Before the activation, ping answers ping: sendto: Network unreachable and returns
code 1. After ip link set lo up, ip -br link prints
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>. The UNKNOWN state is
normal for a loopback interface: what matters is UP between the angle brackets.
The second ping prints 2 packets transmitted, 2 packets received, 0% packet loss. On the host, ip -br link also lists enp5s0 (the name varies
from one machine to another): the container has only lo.
ip runs here with the host's files but in the container's network, since
nsenter joined only that namespace. A namespace can be joined on its own:
without --root, the program keeps the host's root, which is useful for
configuring a container with the host's tools. The ip command of the Alpine
rootfs (BusyBox) does not know the -br option, by the way.
Why does kill -TERM not kill the container's PID 1?
The kernel treats the PID 1 of a namespace as an init process: it ignores the
signals for which that process has installed no handler. Here PID 1 is sleep,
which has none: SIGTERM (the polite stop signal) does nothing, whereas SIGKILL
(which cannot be intercepted) works when it comes from an ancestor namespace, here
the host. This is a kernel rule described in the pid_namespaces(7) manual page.
You need a running container, whose number is in PID. If exercise 1 stopped
yours, start another with the first three lines of the section "How do you see this
container from the host?".
After kill -TERM, ps still prints 4966 S sleep: the process is still
alive. After kill -KILL, the shell reports Killed for the unshare command,
and ps prints only the header of its columns, followed by termine: the process
is gone. The same mechanism explains why a container whose PID 1 does not handle
SIGTERM does not stop cleanly: engines send SIGTERM, wait for a delay, then
send SIGKILL.
What does pivot_root do instead of chroot?
pivot_root replaces the root of the mount namespace and lets you unmount the old
one, whereas with chroot the old root stays mounted, simply out of the process's
reach. This is what runc and crun, the programs engines use to start a
container, generally do. A process that keeps a way to reach the old root can
escape from a chroot; after a pivot_root followed by an unmount, no path leads
to the old root any more in this mount namespace.
You run the sequence in a new mount namespace, which leaves the host untouched.
It starts by making the mounts private (so that nothing propagates back to the
host), makes the rootfs a mount point (pivot_root requires it), swaps the roots,
then unmounts the old one.
Terminal window
unshare--mount--pid--fork--uts/bin/bash-c'
set -e
mount --make-rprivate /
mount --bind /srv/alpine /srv/alpine
cd /srv/alpine
mkdir -p .oldroot
pivot_root . .oldroot
hash -r
cd /
mount -t proc proc /proc
ls /.oldroot | head -5
umount -l /.oldroot
rmdir /.oldroot
ls /
cat /proc/self/mounts
'
Before umount -l, ls /.oldroot still shows bin boot dev etc home: these are
the host's directories, attached under the new /. After the unmount, ls /
shows only the Alpine tree, and /proc/self/mounts contains only two lines:
the root (on the tested VM /dev/sda2 / ext4 rw,relatime 0 0, the device varies)
and proc /proc proc rw,relatime 0 0. The old root is not mounted at all any more.
After the command, cat /etc/debian_version on the host still answers the same
number: nothing changed on its side.
Read the host's system through the old rootExercise 2 of 2 · ~8 min · terminal
Between pivot_root and the unmount, the old root is still attached under /.oldroot. You want to see what a process can read there during this interval, see why ordinary commands do not remove it, then check that umount -l cuts the access.
Do the pivot without unmounting
Take the sequence of the previous section up to the mount of /proc, without the umount -l. Read the ID and VERSION_ID lines of /etc/os-release, first in the new root, then under /.oldroot.
Try to remove the old root without -l
Run rmdir /.oldroot, then umount /.oldroot without the -l option. Read each error message. The sequence of the previous section stops at the first failure because of set -e: if you replay it as is, add || true after each command that must fail.
Unmount with -l and check
Unmount the old root with umount -l, remove the directory, count the lines of /proc/self/mounts that contain oldroot, then try to list /.oldroot.
unshare
pivot_root
umount
A hint 1 / 1
Every filesystem of the host stays mounted under /.oldroot as long as it is not unmounted. grep -c counts the lines that match a pattern.
The new root describes Alpine; /.oldroot/etc/os-release describes the host's system, so the process reads its files. The two commands of step 2 fail as long as filesystems are mounted under /.oldroot: their messages end with Resource busy. This is what explains the -l (lazy), which detaches the old root immediately and releases it when nothing uses it any more. After umount -l and rmdir, the count is 0 and ls answers No such file or directory. As long as the old root is not unmounted, pivot_root alone does not isolate you from the host's system: it is the unmount that cuts the access, which chroot does not do.
The || true prevents set -e from stopping the script on a command that fails on purpose. A detail with no consequence for what follows: these messages are BusyBox's, since rmdir and umount come here from the Alpine rootfs, whose musl library writes Resource busy where glibc writes Device or resource busy. The umount of util-linux, on the host, would print target is busy instead.
What is still missing from this container?
This hand-made container is isolated, but it remains incomplete: five gaps
separate it from what an engine delivers. Knowing them keeps you from believing
that an unshare command is enough in production.
The first gap is the /dev directory, empty in this rootfs. Without /dev/null, a
redirection yes > /dev/null does not find the file, and the shell creates an
ordinary file named null, which grows as long as the command runs. This file
grows fast: with timeout 3 chroot /srv/alpine /bin/sh -c 'yes > /dev/null', it
exceeds 450 MB in three seconds on the tested VM. Never leave this command running.
The fix is to create the real device files with mknod, where c designates a
character device and the two numbers (1 and 3 for null) identify the kernel's
driver:
Terminal window
rm-f/srv/alpine/dev/null
mknod-m666/srv/alpine/dev/nullc13
mknod-m666/srv/alpine/dev/zeroc15
mknod-m666/srv/alpine/dev/randomc18
mknod-m666/srv/alpine/dev/urandomc19
ls -la /srv/alpine/dev must then print four lines starting with crw-rw-rw-, for
example crw-rw-rw- 1 root root 1, 3 ... null. The other four gaps are the
following:
To finish, stop the containers that are still running (pgrep -a sleep, then
kill -KILL). On a disposable virtual machine, /srv/lab and /srv/alpine can
stay; otherwise, delete them with rm -r /srv/lab /srv/alpine, then leave the root
shell with exit. The deletion comes before exit: without root rights, it would
be refused (Permission denied).
Troubleshooting
These are the symptoms to know when a command of the lesson does not give the
expected result. For the others, start by checking that you are root (the prompt
ends with #) and that /srv/alpine was not modified.
Symptom
Cause
Fix
sha256sum -c prints FAILED, then WARNING: 1 computed checksum did NOT match
Corrupted or tampered archive
Delete it, download it again, never extract it
ps in chroot prints only the header PID COMMAND, with no error
/proc is not mounted in the rootfs
mount -t proc proc /srv/alpine/proc, then umount after use
The hostname also changes on the host
--uts forgotten in unshare
Run again with --uts; check with hostname on the host
ps in unshare prints only the header PID USER COMMAND
--mount-proc forgotten: the rootfs's /proc stays empty (--mount-proc also creates the mount namespace)
Add --mount-proc
nsenter --all shows systemd and cat /etc/alpine-release fails
Host root kept
Add --root --wd
kill -TERM has no effect on PID 1
The kernel ignores the signal without a handler
Use kill -KILL
A huge /srv/alpine/dev/null file appears
Redirection to a missing /dev/null: an ordinary file was created
rm -f, then mknod -m 666 ... null c 1 3
unshare or mount refuses the operation, or lsns -p does not list the container
The command was not run from a root shell
Open a root shell with sudo -i, then run it again
The container started by unshare … & is still running after the SSH session is closed
Observed on Debian 13: the container started by unshare … & from a root shell is not stopped when the SSH session closes
Find it with pgrep -a sleep or lsns, then stop it with kill -KILL
Swipe to see more
Put it into practice
In this lab, you take the building blocks one by one on a Debian 13 VM without a
container engine: one namespace at a time, a --pid without --mount-proc, a
SHA-256 check with a sums file, a detached container and the numbers of a device.
It ends with a challenge to do on your own: prepare a rootfs verified by its sum,
start an isolated process under the requested machine name, as PID 1, with a real
/dev/null. The validation checks the state of the machine, not the commands you
typed.
Key points
A container is a process of the host, with namespaces and a root of its own: there is no separate kernel and no emulated machine.
chroot changes only /: hostname, processes and network stay those of the host.
unshare --pid requires --fork, and --mount-proc mounts /proc again so that ps sees only the container.
Seen from the host, the container has a normal PID; lsns and /proc/PID/ns/ show which namespaces are its own or shared (here user, cgroup and time are shared).
nsenter --all does not change the root: you need --root --wd to be really inside.
The PID 1 of a namespace ignores SIGTERM without a handler; SIGKILL from the host works. Engines therefore send SIGTERM then SIGKILL after a delay.
pivot_root followed by an unmount unmounts the old root, which chroot does not do: this is what runc and crun generally do.
This container has no correct /dev, no limits, no privilege restriction, no network and no image.
Knowledge check
This quiz asks six questions drawn from twelve. It checks that you can explain
what a container is and read the outputs of this lesson.
Knowledge check
Check what you have learned with this interactive quiz
6 questions
6 min
70% to pass
How it works
The timer starts when you click Start
Multiple choice, true/false and short answer questions
You can move between questions
Detailed results are shown at the end
Starts the quiz and the timer
⏱️--:--
0 / 6
Review
(0/0)
--:--
0%
Result saved on this device. It will be sent to your account at the next sync. Sync now
0Correct
0To review
0:00Time
-Level
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.