runc is an OCI runtime: a program that creates a container from a folder called
a bundle. It does only what you did by hand with unshare: it reads a
filesystem (rootfs) and a config.json file, then creates the isolated
process that the file describes.
This lesson is for beginners who have isolated a process with unshare and want
to see how a tool does it for them. You will build a bundle, read its
config.json field by field, run a container, follow its lifecycle, rerun the
bundle with crun, produce it from an image with umoci, then run it under a
non-root account.
What you will learn
- Build an OCI bundle with
runc specand understand it. - Map each field of
config.jsonto the manual step it replaces. - Start a container with
runc run, then follow its lifecycle. - Rerun the same bundle with crun, and produce one from an image with umoci.
- Run the container under a non-root account and read its capabilities.
- Diagnose three common runc errors.
Prerequisites
This lesson of the Docker training starts no Docker container: it stops at the program that creates the process. Three readings prepare it.
- Isolate a process by hand with namespaces: A container by hand with unshare and nsenter.
- Limit its resources with cgroups and drop capabilities: Limit a container.
- Have read an OCI image on disk: Read an OCI image and verify its digest produces the
/srv/ocilayout that this lesson turns into a bundle, with the imagealpine:3.24.2@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6. The vocabulary (manifest, layer, digest) is defined in Understand OCI.
You also need a disposable Debian 13 virtual machine, with root rights, and the packages installed by apt install runc crun umoci jq. Tested versions: runc 1.1.15 (spec 1.2.0), crun 1.21, umoci 0.4.7, jq 1.7. The outputs in this lesson come from these versions on Debian 13.
What are an OCI runtime and a bundle?
An OCI runtime is a program that creates a container from a bundle. A
bundle is a folder that follows the Runtime Specification of the
Open Container Initiative (OCI), the
organization that standardizes container formats. It contains at least two
things: a rootfs folder, the filesystem the container will see as its root
(the name rootfs is a convention), and a config.json file, which describes
what to run and how to isolate it. A tool can add its own files next to them, as
umoci does in its own section.
Two runtimes are common. runc is written in Go and comes from the code that Docker gave to the OCI in 2015: it is the historical runtime. crun is an implementation written in C. The official repositories are opencontainers/runc and containers/crun; the specification is read at opencontainers/runtime-spec. Both accept the same bundle, which the crun section checks.
Above all, remember what runc does not do. It downloads no image, manages
neither the network between containers nor image storage, and has no pull or
build command. These functions belong to the higher-level engines, presented
in
Runtime, engine and orchestrator.
Kubernetes asks for a container to be started through the CRI (Container Runtime Interface, the interface it uses to talk to engines), and an intermediate component gives runc a ready-made bundle. Sandboxed runtimes such as gVisor or Kata Containers replace runc with another program: they are described in Runtime Sandboxes.
Look at the bundle, outlined in bold in the middle of the diagram: it separates the image side (above) from the execution side (below). The runtime needs nothing else to create the process.
How do you build a bundle with runc spec?
You build a bundle by extracting the image layer into a rootfs folder, then
asking runc spec to write a default config.json. This manual method shows
that a bundle is nothing magic: it is an unpacked tar archive and a text file.
Only the first layer is extracted here: the Alpine image has just one, and an
image with several layers needs umoci, which extracts them all (see the umoci
section).
The first step reads the layout index to find the manifest, then the digest of
the first layer (the LAYER= line), and extracts that layer, a compressed
tar archive, into rootfs. The second step generates the config.json.
-
Extract the layer into
rootfs.Terminal window B=/srv/bundlemkdir -p "$B/rootfs"LAYER=$(jq -r '.layers[0].digest' /srv/oci/blobs/sha256/$(jq -r '.manifests[0].digest' /srv/oci/index.json | cut -d: -f2) | cut -d: -f2)tar -xzf /srv/oci/blobs/sha256/$LAYER -C "$B/rootfs" -
Generate the default
config.json.Terminal window cd "$B"runc specls -la "$B"
The listing should show a rootfs folder and a config.json file
of 2,588 bytes, or 177 lines according to wc -l config.json. The bundle looks
like this:
Répertoirebundle/
- config.json
Répertoirerootfs/
Répertoirebin/
- …
Répertoiredev/
- …
Répertoireetc/
- …
Répertoireproc/
- …
Répertoiresys/
- …
Répertoireusr/
- …
The rootfs tree is abridged: it shows the folders of a minimal Alpine. Note
that config.json sits next to rootfs, not inside it: the container never
sees this file, which only describes its environment.
What does config.json contain, field by field?
config.json describes in JSON everything you used to do by hand: the process
to run, the root filesystem, the mounts, the namespaces and the rights. Each
field replaces a step from the previous lessons. Read it in blocks with jq,
so you do not scroll through its 177 lines in one go.
The first block groups the process and the root.
jq '{ociVersion, process: {terminal: .process.terminal, user: .process.user, args: .process.args, cwd: .process.cwd, env: .process.env, caps_bounding: .process.capabilities.bounding, noNewPrivileges: .process.noNewPrivileges}, root, hostname}' config.jsonThe output, abridged, contains these values:
"ociVersion": "1.2.0"and"terminal": true;- a user with
uid0 andgid0, the command"args": ["sh"]and"cwd": "/"; - a
PATHandTERM=xterminenv; - three capabilities kept:
CAP_AUDIT_WRITE,CAP_KILLandCAP_NET_BIND_SERVICE; noNewPrivilegesset totrue;- a root
{"path": "rootfs", "readonly": true}, so read-only; - a
hostnameequal torunc.
Keep in mind the three values that limit the container: the read-only root,
the three capabilities and noNewPrivileges. The table below ties them to the
manual steps.
The second block lists the mounts and the linux part.
jq '[.mounts[] | {destination, type}]' config.jsonjq '.linux | {namespaces, resources, maskedPaths: (.maskedPaths|length), readonlyPaths: (.readonlyPaths|length)}' config.jsonThe seven default mounts are /proc (type proc), /dev (tmpfs),
/dev/pts (devpts), /dev/shm (tmpfs), /dev/mqueue (mqueue), /sys
(sysfs) and /sys/fs/cgroup (cgroup). The six namespaces are pid,
network, ipc, uts, mount and cgroup. The resources contain a device
rule {"allow": false, "access": "rwm"} (deny everything), and jq counts 10
masked paths and 5 read-only paths.
The sixth namespace, cgroup, appears in the list only on a cgroup v2 host,
which is the case on Debian 13. On a cgroup v1 host, runc spec does not write
this namespace.
The table below ties each field to its default value and to the manual step it replaces, with the lesson where you did that step.
config.json field | Default value from runc spec | Equivalent manual step |
|---|---|---|
process.args, process.cwd, process.env | ["sh"], /, PATH and TERM | The command passed after chroot or unshare, with its directory and its environment (by hand) |
process.user | uid 0, gid 0 | The account the shell ran under: root |
process.capabilities.bounding | 3 capabilities | setpriv --bounding-set (limit) |
process.noNewPrivileges | true | None in the previous lessons: the kernel then forbids the process from regaining rights by running a setuid binary (limit) |
root.path, root.readonly | rootfs, true | --root=/srv/alpine of unshare, or chroot then pivot_root to the rootfs (by hand). The hand-made rootfs stayed writable: read-only is something runc adds |
hostname | runc | The hostname command run in a UTS namespace (--uts) (by hand) |
mounts | 7 mounts | --mount-proc of unshare or mount -t proc for /proc (by hand). The other mounts (/dev, /sys, devpts…) had no complete equivalent: you only had mknod |
linux.namespaces | pid, network, ipc, uts, mount, cgroup (the last one on cgroup v2 only) | The unshare options |
linux.resources | devices denied (deny rwm) | Writes to the cgroup, such as memory.max for memory (limit). The default device rule has no manual equivalent |
linux.maskedPaths, linux.readonlyPaths | 10 and 5 paths | None: maskedPaths hides sensitive paths of /proc and /sys so that they cannot be read, readonlyPaths makes them readable only. The runtime applies this for you |
One absence deserves a remark: the user namespace is not in the list. By
default, runc therefore creates no user namespace, and the container's root is
the host's root, restricted by its capabilities.
The notions of namespaces, cgroups and capabilities are covered in detail in the matching Linux pages.
How do you run the container with runc run?
runc run <identifier> reads the config.json of the current folder, creates
the container, runs the process in the foreground and removes it as soon as
that process ends. It is the simplest command: it chains creation, start and
cleanup on its own.
Before you run it, adapt the file with jq in three changes.
process.terminal becomes false (no interactive console in a script),
process.args becomes a small script that prints what the container sees, and
memory is limited to 64 MiB (67108864 bytes) in
linux.resources.memory.limit.
jq '.process.terminal=false | .process.args=["/bin/sh","-c","echo hostname: $(hostname); id; ps -o pid,comm; cat /etc/alpine-release; ls /dev; grep -E \"CapEff|CapBnd\" /proc/self/status; cat /sys/fs/cgroup/memory.max 2>&1; touch /fichier 2>&1 || true"] | .linux.resources.memory={"limit":67108864}' config.json > config.tmp && mv config.tmp config.jsonrunc run demoThe output should show, in this order:
- the hostname
runc; uid=0(root) gid=0(root);- a process list where
shhas PID 1 andpsa single-digit PID (9 in the test, it varies); - Alpine version
3.24.2; - the contents of
/dev; CapEffandCapBnd, both equal to0000000020000420;67108864, read frommemory.max;touch: /fichier: Read-only file system.
Then runc list shows nothing: the container disappeared with its process.
Each line ties back to a field in the previous table. The name runc comes from
hostname, PID 1 from the pid namespace. The value 0000000020000420 is the
hexadecimal mask of the three capabilities kept (CAP_KILL,
CAP_NET_BIND_SERVICE and CAP_AUDIT_WRITE). The 67108864 comes from the
memory limit you added, and the write refusal comes from root.readonly.
The contents of /dev are populated (null, zero, random, urandom,
pts, shm, tty and others), unlike those of the hand-made container, which
has only what you mount in it. The mounts field and the creation of special
files do this work for you.
What is the lifecycle of a runc container?
The Runtime Specification defines four states:
- creating: a transient state, during
create; - created: the process exists and waits for the start order;
- running: it runs the command;
- stopped: it has ended, but its state stays readable.
After delete, no state exists anymore. The specification also defines five
operations: state, create, start, kill and delete. runc run, seen in
the previous section, chains create, start and delete. runc exec is not
one of them: it is a command specific to runc.
Follow the path from top to bottom: the running state has a bold outline. runc exec, an extension of runc that is not in the specification, adds a process to a running container without changing its state, which is why it does not appear in the diagram.
To play through this cycle, replace the bundle's command with sleep 300 so that you have a process that lasts.
jq '.process.args=["sleep","300"]' config.json > config.tmp && mv config.tmp config.jsonrunc create --pid-file /tmp/demo.pid demorunc listrunc state demo | jq '{ociVersion, id, status, pid, bundle}'After runc create, runc list shows demo with the status created, with a
PID (7625 in the test, it changes at each launch) and the bundle
/srv/bundle. runc state returns JSON where ociVersion is 1.2.0, status
is created and bundle is /srv/bundle.
On the host side, ps -o pid,ppid,stat,comm -p "$(cat /tmp/demo.pid)" shows a
process named runc:[2:INIT]. It is the container's initialization: it has
already created the namespaces and waits for the start order, the bundle's
command has not started yet.
runc start demorunc state demo | jq -r .statusrunc ps demorunc exec demo /bin/sh -c 'ps -o pid,comm; hostname'After runc start, the status is running and runc ps demo shows
sleep 300 (PID 7625 seen from the host, the same as above).
runc exec starts a second process in the existing container: the output lists
sleep (PID 1), sh (PID 6) and ps (PID 11), then runc for the hostname.
The sh has therefore joined the same namespaces, the way nsenter does in the
lesson on the hand-made container.
From the host, the container is just a process. These three commands show its namespaces, its cgroup and the folder where runc keeps its state:
lsns -p "$(cat /tmp/demo.pid)" -o NS,TYPE,PID,COMMANDcat /proc/$(cat /tmp/demo.pid)/cgroupls /run/runc/demolsns shows six namespaces specific to the process: mnt, uts, ipc,
pid, net and cgroup. The time and user namespaces remain the host's
(those of /sbin/init). The /proc/PID/cgroup file gives
0::/system.slice/demo, and reading memory.max in that cgroup returns
67108864. The beginning of the path depends on the cgroup of the shell that
started runc. Started from an SSH session, the same test gives
0::/user.slice/user-1001.slice/demo (tested with runc 1.5.3).
The container's state lives in /run/runc/demo/state.json, a file that runc
rereads at each command: this is what makes it possible to drive a container
from separate invocations.
runc kill demo KILLrunc listrunc delete demorunc listAfter runc kill demo KILL, the status becomes stopped with a PID of 0:
the process is dead but the entry stays listed. Only runc delete erases it.
runc list is then empty, and the container's cgroup, read earlier in
/proc/PID/cgroup under /sys/fs/cgroup, is gone (ls answers
No such file or directory).
Does crun run the same bundle as runc?
Yes: crun reads the same config.json and the same rootfs, without
conversion, because both programs implement the same specification. Change the
bundle's command, then run crun run.
jq '.process.args=["/bin/sh","-c","echo crun: $(hostname); ps -o pid,comm"]' config.json > config.tmp && mv config.tmp config.jsoncrun run demo-crunThe output should show crun: runc then a ps where the only visible process
is itself, as PID 1. The return code is 0 and crun list is empty afterwards.
The bundle is therefore portable from one runtime to the other. The hostname
runc is not the name of the program: it is the default value that runc spec
wrote in the file.
Choosing between the two is a matter of integration, not compatibility. Podman uses crun or runc depending on its configuration, as the page Podman concepts (in French) shows, and Docker relies on runc. Which program calls which runtime is the subject of the lesson Runtime, engine and orchestrator.
How do you go from an OCI image to a bundle without copying everything by hand?
umoci converts an image from an OCI layout into a bundle in one command:
umoci unpack extracts the layers into rootfs and writes a config.json from
the image's configuration, where runc spec writes generic values. It spares
you the manual extraction of the runc spec section.
umoci unpack --image /srv/oci:3.24.2 /srv/bundle-umocils /srv/bundle-umociThe resulting folder contains four items:
config.json;rootfs;- an
.mtreefile, heresha256_d56c381f961d307a21b3ca004cf1e3910f106644aefb1f43e654c8a56c4fd395.mtree, which reuses the digest of the image manifest for the amd64 architecture. It is an inventory ofrootfs: for each file, type, size, owner, mode and SHA-256 fingerprint, which makes it possible to detect a later modification; - a
umoci.jsonmetadata file, which keeps the original manifest and the extraction options, for example"rootless":false.
The difference with runc spec shows in process. The umoci config.json
reuses the image's command: args is ["/bin/sh"] (the Cmd of the Alpine
image), and env adds HOME (/root) to PATH and TERM. The working
directory stays /. As for the first bundle, set terminal to false and
replace the command for a test, then run:
cd /srv/bundle-umocijq '.process.terminal=false | .process.args=["/bin/sh","-c","echo depuis umoci; cat /etc/alpine-release"]' config.json > c.tmp && mv c.tmp config.jsonrunc run demo-umociThe output should show depuis umoci then 3.24.2, with a return code of 0.
Another difference shows in root. Read it from the bundle folder:
jq .root config.jsonThe output is {"path": "rootfs"}. The readonly field is absent, whereas runc spec wrote "readonly": true.
Without it, the container's root stays writable: a touch /fichier succeeds there with umoci 0.4.7 and runc 1.1.15 on Debian 13. To get read-only back with a umoci bundle, add "readonly": true in root.
Run the container under an unprivileged account
The container in the runc run section ran as root. You want to run it with the ID 65534 (user and group), which Alpine's /etc/passwd file maps to an unprivileged account, and read what this changes for the capabilities. Work in /srv/bundle.
Change the process user
Replace process.user with a uid and a gid of 65534 using jq, then read the field back.
Print the identity and the capabilities
Give process.args a script that runs id, then reads CapEff and CapBnd in /proc/self/status, as in the runc run section. Keep process.terminal set to false.
Run and compare
Run the container with runc run and compare the two masks with those of the container run as root.
- config.json
- process.user
- runc run
A hint 1 / 1
CapEff is the capabilities the process can use now, CapBnd the ceiling of those it could obtain. Compare them separately.
I have tried: show the solution
cd /srv/bundlejq '.process.user={"uid":65534,"gid":65534} | .process.args=["/bin/sh","-c","id; grep -E \"CapEff|CapBnd\" /proc/self/status"]' config.json > config.tmp && mv config.tmp config.jsonjq -c .process.user config.jsonrunc run nobodyOn the tested VM (Debian 13, runc 1.1.15), jq reads back {"uid":65534,"gid":65534} and the container prints:
uid=65534(nobody) gid=65534(nobody)CapEff: 0000000000000000CapBnd: 0000000020000420The account is called nobody because that is what rootfs/etc/passwd maps 65534 to. CapEff drops to zero while CapBnd keeps the value from the runc run section: without ambient capabilities or file capabilities, a program started under a non-zero UID starts with CapEff at zero, and the bundle's ceiling stays the same. An ambient capability is kept when a process runs a program; a file capability is set on the binary itself. The process.capabilities.ambient field of config.json lets you keep some, provided you also list the same capability in permitted and inheritable. With ambient alone, runc 1.5.3 prints can't raise ambient capability CAP_NET_BIND_SERVICE: operation not permitted and CapEff stays at zero; with all three sets, CapEff is 0000000000000400 (tested on Debian 13).
Troubleshooting
runc's messages name the identifier, the file or the program at fault: read them in full before changing anything. Here are three errors you may meet while following this lesson.
| Symptom | Cause | Solution |
|---|---|---|
runc create failed: container with given ID already exists | The identifier passed to runc create or runc run is already taken by a created or stopped container, whose state remains in /run/runc | runc list to see it, then runc delete <identifier> (or runc delete -f <identifier> if it is running) before reusing the identifier |
runc run failed: open /dev/tty: no such device or address | process.terminal is true but the program is not started from a terminal (script, CI, redirection) | Set process.terminal to false for a non-interactive launch, or run the command from a real terminal |
runc run failed: unable to start container process: exec: "/bin/bash": stat /bin/bash: no such file or directory | process.args points to a binary missing from the rootfs: the Alpine image does not contain bash | Use a binary that is present (/bin/sh), which you can check with ls rootfs/bin |
After a container was stopped abruptly, check runc list before running again.
A stopped container still holds its identifier until runc delete has been
run, and its state stays in /run/runc.
Put it into practice
The lab runs on a Debian 13 without a container engine, with runc, umoci, skopeo and jq. You pull a bundle from a busybox image that is already present, set its config.json to run sleep infinity with a machine name and a read-only root, then start the container detached. A first stopped attempt still holds the identifier: it is up to you to remove it. The validation checks the config.json and the container state according to runc, then runs hostname and attempts a write in the container with runc exec.
Key points
- An OCI bundle is a folder with a
rootfsand aconfig.json, defined by the Runtime Specification. - A low-level runtime (runc, crun) only creates the isolated process described by the bundle: no image download, no network, no storage.
- Each field of
config.jsonreplaces a manual step: namespaces, cgroup, capabilities, read-only root, mounts. runc specwrites a genericconfig.json, with a read-only root;umoci unpackwrites one from the image's configuration, withoutreadonly: the root stays writable.- Without ambient capabilities or file capabilities, a program started under a
non-zero UID starts with
CapEffat zero;CapBndkeeps the value set by the bundle. An ambient capability applies only if it is also inpermittedandinheritable. runc runchains everything and removes the container at the end;create,start,killanddeleteseparate the steps, and a stopped container keeps its identifier untilrunc delete.- The same bundle runs with crun, without conversion.
- Seen from the host, a container is just a process with its namespaces and its cgroup.
Knowledge check
The bank has twelve questions on the bundle, the config.json, the lifecycle and
crun; six are drawn at each attempt.
Knowledge check
Check what you have learned with this interactive quiz
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
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
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.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
Continue the course: Runtime, engine, orchestrator: who does what?
A new full quiz with random questions
Work again only on the questions you missed