Skip to content

27. A hosts file under runsc

27. A hosts file mounted at /etc/hosts under runsc

Learning goal: whether a file the warden writes can stand in for containerd's own /etc/hosts in a gVisor pod, so service names resolve to the pod's loopback (design, 'Names'). The fallback, if not, is that service names don't resolve and Compose files that rely on them need an override.

Method: spike/compose_pod/main.go hosts, through internal/cri's client and containerd's CRI with handler runsc, on smaug (Debian 13.8, containerd 1.7.24, gVisor release-20261116.0, as the developer in the socket's group) and in the Mac runner's Lima VM (Debian 13 arm64, the same containerd and gVisor, as root). One pod, three containers running sleep infinity: docker.io/library/debian:trixie-slim (glibc) and docker.io/library/alpine:3.22 (musl), each with a hosts file mounted read-only at /etc/hosts through ContainerConfig.Mounts, and a second Debian container without the mount as a control. The file holds 127.0.0.1 localhost svc-a svc-b and the same names for ::1. Each check runs in the container through CRI ExecSync.

No predictions were written down before the run.

Results (2026-11-30)

Identical on smaug and in the Mac VM:

Check glibc (Debian) musl (Alpine) control (no mount)
cat /etc/hosts our file, exactly our file, exactly containerd's: the host's /etc/hosts (smaug: 127.0.1.1 smaug; the VM: 192.168.5.2 host.lima.internal)
/etc/hosts in /proc/mounts 9p ro 9p ro 9p rw
getent hosts svc-b ::1 localhost svc-a svc-b, exit 0 ::1 localhost localhost svc-b, exit 0 exit 2 (not found)
getent ahostsv4 svc-b 127.0.0.1 127.0.0.1
getent hosts svc-a, localhost ::1, exit 0 ::1, exit 0
echo x > /etc/hosts as root Read-only file system Read-only file system

A first run in the Mac VM failed to start the Debian container with failed to load /usr/bin/sleep: exec format error. Every one of the 2,677 files in that image's unpacked layer was empty: the VM's containerd held a corrupt snapshot of debian:trixie-slim from before this session (the cause wasn't found; a VM stopped soon after an unpack, before the files reached the disk, would look like this). Removing the image and its snapshot (ctr -n k8s.io images rm, ctr -n k8s.io snapshots rm) and pulling again fixed it; nothing about runsc. Nothing notices such a snapshot until a job uses the image.

What we learned

  • A mount at /etc/hosts replaces containerd's file under runsc, and stays read-only to root in the container. Containerd's own file is the host's /etc/hosts, copied, which names the host (smaug, host.lima.internal); the warden's file drops that too.
  • Both C libraries resolve names from it: glibc and musl alike, IPv4 and IPv6. getent hosts answers with ::1 first, so a client that tries addresses in order reaches a service on ::1 before 127.0.0.1; a service listening on IPv4 only costs that client one refused connection per lookup.

What it changes

  • The design's 'Names' stands as written: the warden writes one hosts file per run and mounts it read-only at /etc/hosts in every container.
  • A corrupt image snapshot in a runner's containerd breaks every job using that image until someone removes it by hand. Worth a kata of its own; outside this design.