Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Run an OCI bundle with runc and read its config.json

By updated

Improve this page

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 spec and understand it.
  • Map each field of config.json to 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.

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.

Chain from image to process: an OCI image (layout /srv/oci) is turned into a bundle (rootfs folder and config.json file) by umoci unpack, or by a manual extraction followed by runc spec; runc run, or crun run, reads this bundle and creates the isolated process with its namespaces, its cgroup and its capabilities.OCI imagelayout /srv/ociumoci unpackor manual extraction + runc specBundlerootfs/ + config.jsonrunc run(or crun run)Isolated processnamespaces, cgroup, capabilitiesStorageComponentApplicationFlowData
From image to process: the bundle is the only object the runtime understands. Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

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.

  1. Extract the layer into rootfs.

    Terminal window
    B=/srv/bundle
    mkdir -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"
  2. Generate the default config.json.

    Terminal window
    cd "$B"
    runc spec
    ls -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.

Terminal window
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.json

The output, abridged, contains these values:

  • "ociVersion": "1.2.0" and "terminal": true;
  • a user with uid 0 and gid 0, the command "args": ["sh"] and "cwd": "/";
  • a PATH and TERM=xterm in env;
  • three capabilities kept: CAP_AUDIT_WRITE, CAP_KILL and CAP_NET_BIND_SERVICE;
  • noNewPrivileges set to true;
  • a root {"path": "rootfs", "readonly": true}, so read-only;
  • a hostname equal to runc.

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.

Terminal window
jq '[.mounts[] | {destination, type}]' config.json
jq '.linux | {namespaces, resources, maskedPaths: (.maskedPaths|length),
readonlyPaths: (.readonlyPaths|length)}' config.json

The 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 fieldDefault value from runc specEquivalent manual step
process.args, process.cwd, process.env["sh"], /, PATH and TERMThe command passed after chroot or unshare, with its directory and its environment (by hand)
process.useruid 0, gid 0The account the shell ran under: root
process.capabilities.bounding3 capabilitiessetpriv --bounding-set (limit)
process.noNewPrivilegestrueNone in the previous lessons: the kernel then forbids the process from regaining rights by running a setuid binary (limit)
root.path, root.readonlyrootfs, 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
hostnameruncThe hostname command run in a UTS namespace (--uts) (by hand)
mounts7 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.namespacespid, network, ipc, uts, mount, cgroup (the last one on cgroup v2 only)The unshare options
linux.resourcesdevices 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.readonlyPaths10 and 5 pathsNone: 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.

Terminal window
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.json
runc run demo

The output should show, in this order:

  1. the hostname runc;
  2. uid=0(root) gid=0(root);
  3. a process list where sh has PID 1 and ps a single-digit PID (9 in the test, it varies);
  4. Alpine version 3.24.2;
  5. the contents of /dev;
  6. CapEff and CapBnd, both equal to 0000000020000420;
  7. 67108864, read from memory.max;
  8. 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.

Lifecycle of a runc container from top to bottom: with no container, runc create demo leads to created; runc start demo leads to running; runc kill demo KILL leads to stopped; runc delete demo leads to deleted.runc create demorunc start demorunc kill demo KILLrunc delete demoNo container(bundle only on disk)createdrunc:[2:INIT] process waitingrunningthe config.json process runsstoppedPID 0, state still presentDeletedstate and cgroup removedWaitingExecutionFlow
The observed states and the runc commands that move from one to the next. Source · SVG
Esc to close, + and − to zoom, scroll to move around. 100 %

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.

Terminal window
jq '.process.args=["sleep","300"]' config.json > config.tmp && mv config.tmp config.json
runc create --pid-file /tmp/demo.pid demo
runc list
runc 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.

Terminal window
runc start demo
runc state demo | jq -r .status
runc ps demo
runc 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:

Terminal window
lsns -p "$(cat /tmp/demo.pid)" -o NS,TYPE,PID,COMMAND
cat /proc/$(cat /tmp/demo.pid)/cgroup
ls /run/runc/demo

lsns 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.

Terminal window
runc kill demo KILL
runc list
runc delete demo
runc list

After 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.

Terminal window
jq '.process.args=["/bin/sh","-c","echo crun: $(hostname); ps -o pid,comm"]' config.json > config.tmp && mv config.tmp config.json
crun run demo-crun

The 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.

Terminal window
umoci unpack --image /srv/oci:3.24.2 /srv/bundle-umoci
ls /srv/bundle-umoci

The resulting folder contains four items:

  • config.json;
  • rootfs;
  • an .mtree file, here sha256_d56c381f961d307a21b3ca004cf1e3910f106644aefb1f43e654c8a56c4fd395.mtree, which reuses the digest of the image manifest for the amd64 architecture. It is an inventory of rootfs: for each file, type, size, owner, mode and SHA-256 fingerprint, which makes it possible to detect a later modification;
  • a umoci.json metadata 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:

Terminal window
cd /srv/bundle-umoci
jq '.process.terminal=false | .process.args=["/bin/sh","-c","echo depuis umoci; cat /etc/alpine-release"]' config.json > c.tmp && mv c.tmp config.json
runc run demo-umoci

The 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:

Terminal window
jq .root config.json

The 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 Exercise 1 of 1 · ~10 min · terminal

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.

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
Terminal window
cd /srv/bundle
jq '.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.json
jq -c .process.user config.json
runc run nobody

On 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: 0000000000000000
CapBnd: 0000000020000420

The 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.

SymptomCauseSolution
runc create failed: container with given ID already existsThe identifier passed to runc create or runc run is already taken by a created or stopped container, whose state remains in /run/runcrunc 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 addressprocess.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 directoryprocess.args points to a binary missing from the rootfs: the Alpine image does not contain bashUse 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 rootfs and a config.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.json replaces a manual step: namespaces, cgroup, capabilities, read-only root, mounts.
  • runc spec writes a generic config.json, with a read-only root; umoci unpack writes one from the image's configuration, without readonly: the root stays writable.
  • Without ambient capabilities or file capabilities, a program started under a non-zero UID starts with CapEff at zero; CapBnd keeps the value set by the bundle. An ambient capability applies only if it is also in permitted and inheritable.
  • runc run chains everything and removes the container at the end; create, start, kill and delete separate the steps, and a stopped container keeps its identifier until runc 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

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