One of our clients, a company that builds edge vision devices on an NVIDIA Jetson module, runs every application of its devices as a container. Its update client is a container too, and it drives Docker through the engine socket mounted in its container. Whoever controls that socket is root on the device, so a flaw in the update client is a flaw in the whole system.
Docker and Podman can both run as an ordinary user, and meta-virtualization builds most of what they need. A Yocto image still leaves the setup to the product layer.
The user, its subordinate IDs, a user manager started at boot without a login, the rootless daemon of Docker and the import of the factory image all come from recipes of the product. This article writes them for Docker and Podman on the qemuarm64 machine of Yocto Wrynose, starting from the host image of our article on containers with Yocto, and our introduction to Linux containers explains user namespaces. containerd can run rootless the same way, with the containerd-rootless.sh script that nerdctl ships in its sources, but it is not covered here.
Contents
Root Access Through the Engine Socket
On the rootful Docker host of the Yocto article, the socket belongs to the docker group. A member of that group cannot read /etc/shadow, and the image runs as user 1000, but the client chooses the user of a container:
op@qemuarm64:~$ id uid=1000(op) gid=1000(op) groups=997(docker),1000(op) op@qemuarm64:~$ cat /etc/shadow cat: can't open '/etc/shadow': Permission denied op@qemuarm64:~$ docker run --rm -v /:/host --entrypoint /bin/head frame-counter-image -n 1 /host/etc/shadow head: /host/etc/shadow: Permission denied op@qemuarm64:~$ docker run --rm --user 0 -v /:/host --entrypoint /bin/head frame-counter-image -n 1 /host/etc/shadow root::15069:0:99999:7:::
The update client had the same access through the socket mounted in its container. The socket of containerd gives the same access.
A rootless engine runs as an ordinary user and creates the containers in a user namespace, with a range of subordinate user IDs reserved for that user in /etc/subuid. Root inside a container is then that user on the host, and the other IDs of the container map into the reserved range. The hosts below run the engine for a user app with user ID 1000, the IDs 100000 to 165535, and nothing to set up on the device.
Several pieces then live in the home directory of app and under its systemd user manager instead of under /etc and PID 1.
A Rootless Host Image with Yocto
A second image recipe, vision-host-rootless-image.bb, reuses the host image of the Yocto article and selects the engine with the same CONTAINER_PROFILE:
require vision-host-image.bb
SUMMARY = "Container host image of the vision devices, rootless engine"
# The rootful storage would not be visible to the rootless engine
BUNDLED_CONTAINERS = ""
IMAGE_INSTALL += "${CONTAINER_PROFILE}-rootless frame-counter-rootless"
inherit extrausers
EXTRA_USERS_PARAMS = " \
usermod -v 100000-165535 app; \
usermod -w 100000-165535 app; \
"
# No rootful Docker or containerd daemon next to the rootless one. OE-core applies the
# presets in enable-only mode, so the units are disabled after it.
disable_rootful_docker() {
systemctl --root=${IMAGE_ROOTFS} disable docker.service docker.socket containerd.service
}
IMAGE_PREPROCESS_COMMAND += "${@'disable_rootful_docker' if d.getVar('CONTAINER_PROFILE') == 'docker' else ''}"
BUNDLED_CONTAINERS is emptied because it installs the images in the storage of the rootful engine, /var/lib/docker or /var/lib/containers, while a rootless engine keeps its storage in the home directory of the user. The two packages of IMAGE_INSTALL, podman-rootless or docker-rootless and frame-counter-rootless, are recipes of the product layer written below.
The extrausers class runs EXTRA_USERS_PARAMS on the root file system once the packages are installed. usermod -v and usermod -w reserve the user and group IDs 100000 to 165535 for app in /etc/subuid and /etc/subgid. newuidmap and newgidmap, installed with shadow, apply them when the engine creates a user namespace.
On the Docker host, the docker package still enables the rootful daemon and its socket. OE-core applies the systemd presets with --preset-mode=enable-only, so a preset file cannot disable a unit, and a masked unit made the check of the root file system fail. The image therefore disables the three units in IMAGE_PREPROCESS_COMMAND, which runs once the root file system is complete.
Two lines join the configuration of the build:
DISTRO_FEATURES:append = " pam" PACKAGECONFIG:append:pn-podman:poky = " rootless"
The services of a user run under a systemd user manager, user@1000.service, which gets its environment from the pam_systemd module. poky builds systemd without PAM, and the manager stopped at boot with “Trying to run as user instance, but $XDG_RUNTIME_DIR is not set”. The reference host image of meta-virtualization, container-image-host, lists pam among its recommended distro features for this reason.
The rootless option of the podman recipe, off by default, installs slirp4netns, the user-space network stack of rootless containers, and fuse-overlayfs, raises user.max_user_namespaces with a sysctl file, and makes slirp4netns the default network command in containers.conf. The :poky override limits it to the distro of the host. The vruntime multiconfig, which the layer uses to install images at build time, reads the same local.conf and masks fuse-overlayfs, and failed on “Nothing RPROVIDES ‘fuse-overlayfs'” without the override. A product sets the option in its distro configuration file, which the multiconfig does not read.
Rootless Podman
podman-rootless.bb creates the user and starts its user manager at boot:
inherit useradd allarch
ROOTLESS_USER ?= "app"
ROOTLESS_UID ?= "1000"
USERADD_PACKAGES = "${PN}"
GROUPADD_PARAM:${PN} = "-g ${ROOTLESS_UID} ${ROOTLESS_USER}"
USERADD_PARAM:${PN} = "-u ${ROOTLESS_UID} -g ${ROOTLESS_USER} -d /home/${ROOTLESS_USER} -s /bin/sh ${ROOTLESS_USER}"
RDEPENDS:${PN} = "podman shadow"
do_install() {
install -d -m 0755 ${D}/home/${ROOTLESS_USER}
chown ${ROOTLESS_USER}:${ROOTLESS_USER} ${D}/home/${ROOTLESS_USER}
# start the user manager at boot, without a login
install -d ${D}${localstatedir}/lib/systemd/linger
touch ${D}${localstatedir}/lib/systemd/linger/${ROOTLESS_USER}
}
The useradd class creates the user and its group when the package is installed into the root file system, with a fixed user ID that the next recipe depends on. A file named after the user in /var/lib/systemd/linger is what loginctl enable-linger would create on the device, and makes systemd start the user manager at boot without a login.
frame-counter-rootless installs the OCI archive of the application image in /usr/share/oci and a Quadlet file in /etc/containers/systemd/users/1000, a directory that Quadlet reads for user 1000 only:
[Container] Image=localhost/frame-counter-image:latest Pull=never ContainerName=frame-counter PublishPort=8080:8080 [Service] ExecStartPre=/bin/sh -c "podman image exists localhost/frame-counter-image:latest || skopeo copy oci-archive:/usr/share/oci/frame-counter-image.tar containers-storage:localhost/frame-counter-image:latest" Restart=always RestartSec=5 [Install] WantedBy=default.target
The first start copies the factory image into the storage of app with skopeo, and later starts keep the version that is there. Pull=never stops Podman from looking for localhost/frame-counter-image in a registry. After boot:
app@qemuarm64:~$ export XDG_RUNTIME_DIR=/run/user/1000
app@qemuarm64:~$ podman ps --format "{{.Names}} {{.Image}} {{.Status}} {{.Ports}}"
frame-counter localhost/frame-counter-image:latest Up 10 seconds 0.0.0.0:8080->8080/tcp
root@qemuarm64:~# ps -o pid,ppid,user,args | grep -E "PID|[c]onmon|[s]lirp4netns|[f]rame-counter"
PID PPID USER COMMAND
839 234 app /usr/bin/slirp4netns --disable-host-loopback --mtu=65520 [...]
862 234 app /usr/bin/conmon --api-version 1 -c e1fceea7b56a[...] -r /usr/bin/crun [...]
864 862 100999 /usr/bin/frame-counter camera0
root@qemuarm64:~# echo | nc localhost 8080
frame-counter 1.0.0 camera0 frame 1
slirp4netns and conmon run as app, and the application as user 100999, the ID that user 1000 of the container maps to. The storage is /home/app/.local/share/containers/storage, with the vfs driver that storage.conf of meta-virtualization chooses.
Rootless Docker
meta-virtualization packages rootlesskit, which creates the user namespace and the network of a rootless Docker daemon, but has no rootless setup for Docker. The product layer adds it in three pieces, after the layout of an industrial Linux distribution we worked on. The first is an append to docker-moby that installs the start script of the rootless daemon, shipped in the Docker sources:
# docker-moby_git.bbappend
do_install:append() {
install -m 0755 ${S}/src/import/contrib/dockerd-rootless.sh ${D}${bindir}
}
The second is docker-rootless.bb. It creates the user with the same useradd lines as podman-rootless.bb, with RDEPENDS set to docker rootlesskit slirp4netns iptables iproute2 shadow, and installs the rest:
do_install() {
u=/home/${ROOTLESS_USER}/.config/systemd/user
install -d ${D}$u/default.target.wants
install -m 0644 ${UNPACKDIR}/docker.service ${D}$u/
ln -s ../docker.service ${D}$u/default.target.wants/docker.service
# the rootless daemon reads its configuration and registry certificates
# from the home directory, not from /etc/docker
c=/home/${ROOTLESS_USER}/.config/docker
install -d ${D}$c
ln -s ${sysconfdir}/docker/daemon.json ${D}$c/daemon.json
ln -s ${sysconfdir}/docker/certs.d ${D}$c/certs.d
chown -R ${ROOTLESS_USER}:${ROOTLESS_USER} ${D}/home/${ROOTLESS_USER}
# netfilter modules, loaded at boot by systemd-modules-load
install -D -m 0644 ${UNPACKDIR}/docker-rootless.conf ${D}${sysconfdir}/modules-load.d/docker-rootless.conf
# start the user manager at boot, without a login
install -d ${D}${localstatedir}/lib/systemd/linger
touch ${D}${localstatedir}/lib/systemd/linger/${ROOTLESS_USER}
}
The user unit of the daemon goes into the home directory, with the link that systemctl --user enable would create. It derives from the unit that dockerd-rootless-setuptool.sh of Docker writes on a desktop distribution. Its main lines:
[Service] Environment=PATH=/usr/bin:/sbin:/usr/sbin:/bin ExecStart=/usr/bin/dockerd-rootless.sh Restart=always Delegate=yes Type=notify NotifyAccess=all KillMode=mixed
The upstream unit sets the same PATH, with the directory of the Docker binaries first and /sbin and /usr/sbin added for iptables and ip. Delegate=yes gives the daemon its own cgroup subtree, and Type=notify makes systemd wait until the daemon reports that it is ready.
docker-rootless.conf lists iptable_nat, iptable_filter, ip6table_nat, xt_MASQUERADE and the other netfilter modules that a rootful dockerd loads by itself. A rootless daemon cannot load a module, and without them it stopped with “can’t initialize iptables table `nat'”.
The links in /home/app/.config/docker matter as soon as the device pulls from a registry. The build installs the registry configuration in /etc/docker, daemon.json and the certificate of the registry in certs.d, but a rootless daemon reads its configuration in the home directory only. Without the links, a pull failed:
app@qemuarm64:~$ export XDG_RUNTIME_DIR=/run/user/1000 DOCKER_HOST=unix:///run/user/1000/docker.sock app@qemuarm64:~$ docker pull -q 10.0.2.2:5000/yocto/frame-counter-image:1.1.0 Error response from daemon: failed to resolve reference "10.0.2.2:5000/yocto/frame-counter-image:1.1.0": failed to do request: Head "https://10.0.2.2:5000/v2/yocto/frame-counter-image/manifests/1.1.0": tls: failed to verify certificate: x509: certificate signed by unknown authority
The third piece is the user unit of the application, installed by frame-counter-rootless in /etc/systemd/user with the OCI archive:
[Unit]
ConditionUser=app
After=docker.service
Requires=docker.service
[Service]
Environment=DOCKER_HOST=unix://%t/docker.sock
ExecStartPre=/bin/sh -c "docker image inspect frame-counter-image:latest >/dev/null 2>&1 || { skopeo copy oci-archive:/usr/share/oci/frame-counter-image.tar docker-archive:%t/frame-counter-image.tar:frame-counter-image:latest && docker load -i %t/frame-counter-image.tar; rm -f %t/frame-counter-image.tar; }"
ExecStartPre=-/usr/bin/docker rm -f frame-counter
ExecStart=/usr/bin/docker run --rm --name frame-counter -p 8080:8080 frame-counter-image:latest
ExecStop=/usr/bin/docker stop frame-counter
Restart=always
[Install]
WantedBy=default.target
A unit of /etc/systemd/user applies to every user manager, and ConditionUser keeps it to app. %t is the runtime directory of the user, /run/user/1000, where the rootless daemon puts its socket. docker load reads the OCI archive too, but names the image latest, the only name the archive carries, so skopeo first converts it to a Docker archive with the full name. After boot:
root@qemuarm64:~# systemctl is-enabled docker.service docker.socket disabled disabled root@qemuarm64:~# ps -o pid,ppid,user,args | grep -E "PID|[r]ootlesskit|[s]lirp4netns|[d]ockerd|[c]ontainerd|[f]rame-counter" PID PPID USER COMMAND 236 227 app rootlesskit --state-dir=/run/user/1000/dockerd-rootless --net=slirp4netns [...] 248 236 app /proc/self/exe --state-dir=/run/user/1000/dockerd-rootless --net=slirp4netns [...] 269 236 app slirp4netns --mtu 65520 -r 3 --disable-host-loopback --enable-sandbox [...] 278 248 app dockerd 302 278 app containerd --config /run/user/1000/docker/containerd/containerd.toml 564 227 app /usr/bin/docker run --rm --name frame-counter -p 8080:8080 frame-counter-image:latest 586 227 app /usr/bin/containerd-shim-runc-v2 -namespace moby -id 3ac3caf0eae9[...] 613 586 100999 /usr/bin/frame-counter camera0 root@qemuarm64:~# echo | nc localhost 8080 frame-counter 1.0.0 camera0 frame 3
rootlesskit starts dockerd in the user namespace, and dockerd starts its own containerd. The whole engine runs as app, and no socket owned by root remains.
Updates, Ports and Networks
The update from the registry works as on the rootful hosts, with the commands run as app and the service restarted by the user manager. On the Podman host, which reads the same /etc/containers/registries.conf.d as root:
app@qemuarm64:~$ podman pull -q 10.0.2.2:5000/yocto/frame-counter-image:1.1.0
fbaabe9a8d551e088bd691ca5136c142ff1cf38c97e8beaa2ec2020146e42e69
app@qemuarm64:~$ podman tag 10.0.2.2:5000/yocto/frame-counter-image:1.1.0 localhost/frame-counter-image:latest
app@qemuarm64:~$ systemctl --user restart frame-counter
app@qemuarm64:~$ podman ps --format "{{.Image}} {{.Status}}"
localhost/frame-counter-image:latest Up 11 seconds
root@qemuarm64:~# echo | nc localhost 8080
frame-counter 1.1.0 camera0 frame 4
On the Docker host, with the links of docker-rootless.bb, docker pull of the same image, docker tag to frame-counter-image:latest and systemctl --user restart frame-counter gave the same result. The test needed one more change, specific to QEMU. rootlesskit gives the network namespace of the daemon the range 10.0.2.0/24, the range of QEMU user networking, so the daemon could not reach the registry at 10.0.2.2 and failed with “dial tcp 10.0.2.2:5000: connect: network is unreachable”. A drop-in of the user unit moves the range:
# ~/.config/systemd/user/docker.service.d/cidr.conf [Service] Environment=DOCKERD_ROOTLESS_ROOTLESSKIT_FLAGS=--cidr=10.0.3.0/24
A device whose own network uses 10.0.2.0/24 needs the same drop-in in its image.
Both hosts publish port 8080, because an ordinary user cannot bind a port below /proc/sys/net/ipv4/ip_unprivileged_port_start, 1024 on these hosts. Both engines refuse a lower one:
app@qemuarm64:~$ podman run -d --name lowport -p 80:8080 localhost/frame-counter-image:latest Error: rootlessport cannot expose privileged port 80, you can add 'net.ipv4.ip_unprivileged_port_start=80' to /etc/sysctl.conf (currently 1024), or choose a larger port number (>= 1024): listen tcp 0.0.0.0:80: bind: permission denied
Lowering net.ipv4.ip_unprivileged_port_start with a sysctl file in the image lets every user of the device bind those ports, not only the engine. The message of Docker gives another option, the CAP_NET_BIND_SERVICE capability on the rootlesskit binary.
meta-virtualization After Wrynose
The hosts of this article were built and tested with the wrynose branch of meta-virtualization at commit f27e2594. In master at commit 8f20dd54, rootless Podman is still an option of the recipe, off by default, and Docker still has no rootless setup, so the recipes of this article apply unchanged.
Summing Up
Enable pam in the distro and the rootless option of podman, create the user with a fixed user ID, reserve its subordinate IDs with extrausers, and create its linger file at build time. Install the factory image as an OCI archive and import it into the storage of the user at the first start, with skopeo. For Docker, add dockerd-rootless.sh, the user unit of the daemon, the netfilter modules and the links to /etc/docker, and disable the rootful daemon after the presets.
Check that no root daemon or socket remains, that the application runs under a subordinate ID, and that a pull from the registry works as the user. Watch the ports below 1024, the range 10.0.2.0/24 that rootlesskit takes, and BUNDLED_CONTAINERS, which fills the storage of the rootful engine only.
Do your devices run their container engine as root, and what stops you from moving it? Tell us in the comments.


