Sub topic 6
Setting up QEMU emulation with binfmt_misc
This chapter assumes you know basic Docker usage, can write a simple Dockerfile, and are comfortable with the Linux command line. It also assumes you have read the chapters on target platforms and on builders and drivers.
The problem emulation solves
A CPU runs only machine code for its own architecture. Suppose you build on an amd64 laptop and target linux/arm64. The RUN steps in your Dockerfile execute arm64 binaries such as apt-get and sh. The host kernel cannot run them, so the build fails with exec format error.
QEMU user-mode emulation translates the instructions of a foreign binary into host instructions as the program runs. The Linux kernel feature binfmt_misc decides when to invoke it.
How binfmt_misc works
binfmt_misc is a kernel feature that lets you register interpreters for binary formats. Each registration holds a pattern, usually the ELF header bytes that identify an architecture, and the path of an interpreter. When a process calls exec on a file that matches, the kernel runs the interpreter and passes it the file.
For a foreign architecture, the interpreter is a static qemu-<arch> binary. The program runs as if it were native, only slower.
Registering the handlers
Registrations live in the kernel and are lost on reboot. The tonistiigi/binfmt image writes them for you. It needs --privileged because it modifies kernel state.
docker run --privileged --rm tonistiigi/binfmt --install allThe command registers handlers for every architecture the image supports. To install only one, pass its name, for example --install arm64.
Running the image with no arguments prints the registered emulators as JSON:
docker run --privileged --rm tonistiigi/binfmtYou can also read the kernel's state directly:
ls /proc/sys/fs/binfmt_misc/
cat /proc/sys/fs/binfmt_misc/qemu-aarch64A healthy entry shows enabled, the interpreter path, and the magic bytes that were registered.
Creating a builder that can use it
The default docker driver builder cannot always build several platforms at once. Create a docker-container builder, as the builders chapter described, and start it:
docker buildx create --name multi --driver docker-container --use
docker buildx inspect --bootstrapThe inspect output lists the platforms the builder supports. After you register QEMU, entries such as linux/arm64 and linux/arm/v7 appear next to the host platform. If they are missing, the handlers are not registered, or the builder started before you registered them. Run docker buildx rm multi and create the builder again.
Testing the setup
Run a one-off container for a foreign platform:
docker run --rm --platform linux/arm64 alpine uname -mThe output is aarch64 even on an amd64 host. If you see exec format error, the handler is not active.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `exec format error` during a `RUN` step | No handler registered, or registration lost after reboot | Run `tonistiigi/binfmt --install all` again |
| Platform missing from `buildx inspect` | Builder created before registration | Remove and recreate the builder |
| `operation not permitted` from the binfmt image | Container was not started with `--privileged` | Add `--privileged` |
| Random crashes or hangs in emulated builds | Emulation bugs, often with JIT runtimes or heavy multithreading | Try a newer QEMU, or use native nodes |
Exercise
You register the QEMU handlers and run docker buildx build --platform linux/arm64, but exec format error still appears. The builder was created before you ran the install command. What do you do first, and why does it help?
Show answer
Remove the builder with docker buildx rm multi, then create and bootstrap it again. A builder created before registration may not have detected the new emulated platforms. Then run docker buildx inspect --bootstrap to confirm linux/arm64 is listed.