Can a Docker image boot a real machine?
A few years ago I started pulling the kernel (vmlinuz) and initrd.img out of Linux installs and booting them on their own, with no system attached. What started as poking around became a way of thinking about Linux that I still use. The kernel and initrd are a tractor. A root filesystem is a trailer. Any trailer can be hitched to the tractor, including Kali, Ubuntu, Alpine and, it turns out, Docker images.
Status: working experiment, not a production recipe.
Where it started: the initramfs shell
Boot a kernel and its initrd with no root filesystem, and Linux gets most of the way through startup before it has nowhere to go. On Ubuntu, the script inside the initrd looks for a root device, gives up, and drops you into a BusyBox shell with an (initramfs) prompt.
qemu-system-x86_64 -m 2G -nographic \
-kernel vmlinuz -initrd initrd.img \
-append "console=ttyS0"
It looks like an error screen. It's actually a tiny working Linux system running entirely from RAM. It has a shell, a handful of tools, and the drivers the kernel needs to find disks.
Mounting disks, then chroot
Once I realised that shell could mount disks, I started walking around other systems' files from it. Then I learned about chroot, which makes a directory the new / for a process. If you mount a disk that holds an installed system and chroot into it, you're using that system's programs, libraries and package manager. The kernel underneath came from somewhere else.
(initramfs) mount /dev/sda1 /root
(initramfs) mount -t proc proc /root/proc
(initramfs) chroot /root /bin/bash
I could even start parts of the system from inside the chroot. I could never start all of it, which I come back to below.
The tractor and the trailers
That's where the idea came from. The kernel and initrd do the hard part: they drive the hardware, find the storage and get you to a shell. Whatever I chroot into is just cargo, so I tried different cargo. I hitched a Kali root filesystem to an Ubuntu kernel and initrd, then Alpine to the same pair. Each time, it worked.
Docker images are trailers too
A Docker image is a root filesystem without a kernel. A container never boots; it borrows the host's kernel. So I wondered whether the opposite would work: take an image, give it a kernel, and boot it.
I started with Alpine because it's small. docker export flattens a container (not an image) into a plain tarball of its filesystem, so I ran a container first. Alpine's default command is a shell, which exits straight away with no terminal attached. A stopped container is still enough to export.
sudo docker pull alpine
sudo docker run alpine
sudo docker ps -a # note the container ID
sudo docker export <container-id> -o alpine.tar
Then I wiped a spare drive, formatted the whole device as ext4 with no partition table, and unpacked the tarball onto it.
sudo mkfs -t ext4 /dev/sdb
sudo mount /dev/sdb /mnt
sudo tar xf alpine.tar -C /mnt
sudo umount /mnt
QEMU can use a real drive as its disk, so I gave it the drive along with the kernel and initrd:
sudo qemu-system-x86_64 -m 2G \
-kernel vmlinuz -initrd initrd.img \
-hda /dev/sdb
With no root device on the command line, the initrd dropped me into its shell. Inside the VM the drive appears as /dev/sda. There's no partition number because the filesystem covers the whole device. Alpine has no bash, so the chroot uses /bin/sh:
(initramfs) mount /dev/sda /root
(initramfs) mount -t proc proc /root/proc
(initramfs) chroot /root /bin/sh
That put me in Alpine's userland, running on a kernel from another distribution.
If you follow along, keep three things in mind:
- Check the device name with
lsblkfirst, becausemkfswipes whatever it's pointed at. - Unmount the drive on the host before QEMU starts. An ext4 filesystem mounted by two systems at once will get corrupted.
- No spare drive? A disk image file made with
truncate -s 4G alpine.imgworks the same way. Adding-enable-kvmmakes QEMU much faster.
It worked, which means a setup I've already built and tested as a Dockerfile can leave Docker behind and run under QEMU. Because it's a real drive, it can also go into a real machine; it just needs a bootloader to load the kernel and initrd. The Dockerfile becomes the build script for an operating system.
Many trailers, one disk
Each trailer is just a directory, so several fit on one virtual disk:
trailers.img (ext4)
└── rootfs/
├── kali/
├── ubuntu/
└── alpine/
That gives you one tractor and one disk, and you pick a trailer at boot.
Why it only half-booted
The first thing I noticed inside Alpine was that networking was completely down, even the loopback interface. ifconfig printed nothing until I ran ifconfig lo up. On a normal boot, bringing up loopback is one of the first jobs the init system does. Nothing had done it here, because no init system had run.
chroot changes what a process sees as /. It doesn't change who's in charge.
The first process the kernel starts is PID 1. On a normal system that's the init system, usually systemd. From the initramfs shell, PID 1 still belongs to the initramfs, so the trailer's own init never gets the job. systemd only takes over as the system manager when it's PID 1, so the system came up only partially.
The fix is switch_root (or run-init in Ubuntu's initramfs). Instead of entering the new root as a guest, it frees the initramfs, makes the new filesystem /, and runs the trailer's init as PID 1. The trailer stops being a passenger and becomes the system. switch_root needs the new root to be a mount point, so a subdirectory like rootfs/kali gets bind-mounted to /newroot first.
Swapping engines
I also tried mixing distros the other way round: Kali's userland on an Ubuntu kernel, and Ubuntu's on Kali's. Both booted. The display didn't always work.
That makes sense once you know where drivers live. The core kernel is in vmlinuz, but most drivers are loadable modules, and those sit in the root filesystem under /lib/modules/<kernel version>. Firmware for things like GPUs and Wi-Fi sits in /lib/firmware.
A Kali root filesystem only carries modules for Kali's kernel version. When an Ubuntu kernel boots it and looks for its graphics driver, the matching directory isn't there. Booting still works because the drivers needed to reach the disk come from the initrd, which was built for that exact kernel.
The likely fix is to copy the kernel's matching /lib/modules/<version> directory, plus any firmware it needs, into the trailer. The tractor brings its own tools.
Why Linux lets you do this
The kernel and the distribution are separate things. A distribution is the userland: libc, the init system, the package manager and the config files. The kernel talks to that userland through system calls, and Linux has a long-standing rule against breaking that interface. That's why one kernel can run another distro's userland. It's also why containers work at all. Docker images were always root filesystems waiting for a kernel.
The rule mostly runs one way. Old programs keep working on new kernels, but a very new userland on a very old kernel can need features that don't exist yet. The further apart the tractor and trailer are in age, the more likely something breaks.
Where I used this for real
The second generation of the Rural Kiosk had to run MongoDB on an Intel Celeron that lacked the AVX instructions MongoDB needed. Instead of fighting a full distribution, I built a small system from these same pieces: an Ubuntu kernel pulling an Alpine-based root filesystem that ran a pre-AVX MongoDB, with the root filesystem produced from Docker.
Going further: a real boot
The steps above stop at a chroot. To boot a trailer properly, with its own init as PID 1, swap the distro initrd for a tiny one of your own.
Get a kernel and check its drivers. Copy your kernel out, then check that the drivers this setup needs are built in. =y means built in; =m means a module you'd have to add to the initramfs and load yourself.
sudo cp /boot/vmlinuz-$(uname -r) vmlinuz && sudo chown $USER vmlinuz
grep -E 'CONFIG_(EXT4_FS|VIRTIO_BLK|DEVTMPFS)=' /boot/config-$(uname -r)
Build a trailer. Docker images don't ship an init system, so add one in the Dockerfile:
FROM ubuntu:24.04
RUN apt-get update && apt-get install -y systemd-sysv \
&& echo 'root:changeme' | chpasswd
Then build the image and export it onto a disk image. Two clean-up steps matter here. Docker fills in /etc/hostname at runtime, so the exported copy is empty. And /.dockerenv should go, because systemd uses it to decide it's inside a container and skips parts of a normal boot.
docker build -t trailer-ubuntu .
truncate -s 8G trailers.img
mkfs.ext4 -F trailers.img
sudo mkdir -p /mnt/trailers
sudo mount -o loop trailers.img /mnt/trailers
sudo mkdir -p /mnt/trailers/rootfs/ubuntu
docker create --name t trailer-ubuntu
docker export t | sudo tar -x -C /mnt/trailers/rootfs/ubuntu
docker rm t
sudo rm -f /mnt/trailers/rootfs/ubuntu/.dockerenv
echo tractor-ubuntu | sudo tee /mnt/trailers/rootfs/ubuntu/etc/hostname
sudo umount /mnt/trailers
Build the tractor. This is a minimal initramfs: a static BusyBox and one script. The script reads which trailer to use from the kernel command line, mounts it, and hands over PID 1. Save it as init:
#!/bin/busybox sh
/bin/busybox --install -s /bin
mount -t devtmpfs devtmpfs /dev
mount -t proc proc /proc
mount -t sysfs sysfs /sys
# Defaults, overridable from the kernel command line
TRAILER=ubuntu
DISK=/dev/vda
INIT=/sbin/init
for arg in $(cat /proc/cmdline); do
case "$arg" in
trailer=*) TRAILER="${arg#trailer=}" ;;
disk=*) DISK="${arg#disk=}" ;;
init=*) INIT="${arg#init=}" ;;
esac
done
# Give slow devices (USB) up to 30 seconds to appear
n=0
while [ ! -b "$DISK" ] && [ "$n" -lt 30 ]; do sleep 1; n=$((n + 1)); done
# Hitch the trailer. If anything fails, drop to a shell.
mount -t ext4 "$DISK" /disk || exec sh
mount -o bind "/disk/rootfs/$TRAILER" /newroot || exec sh
umount /disk
# Hand the system over
mount -o move /dev /newroot/dev
umount /proc /sys
exec switch_root /newroot "$INIT"
Then pack it. The BusyBox binary comes from the busybox-static package; file tractor/bin/busybox should say "statically linked".
mkdir -p tractor/bin tractor/dev tractor/proc tractor/sys tractor/disk tractor/newroot
cp /bin/busybox tractor/bin/
sudo mknod -m 600 tractor/dev/console c 5 1
cp init tractor/init && chmod +x tractor/init
(cd tractor && find . | cpio -o -H newc | gzip) > tractor.cpio.gz
Boot it.
qemu-system-x86_64 -enable-kvm -m 2G -nographic \
-kernel vmlinuz -initrd tractor.cpio.gz \
-drive file=trailers.img,format=raw,if=virtio \
-append "console=ttyS0 trailer=ubuntu"
To add another trailer, export another image into rootfs/<name> and boot with trailer=<name>. A trailer with no init system can still be explored by adding init=/bin/sh to the command line. Alpine boots with OpenRC rather than systemd, so install openrc in its Dockerfile.
Bare metal. For a flash drive you need the same pieces plus a bootloader. Use a small partition holding GRUB, vmlinuz and tractor.cpio.gz, and an ext4 partition holding rootfs/. Give each trailer its own GRUB entry:
menuentry "Kali" {
linux /vmlinuz trailer=kali disk=/dev/sda2
initrd /tractor.cpio.gz
}
Two cautions apply on real hardware. Device names aren't guaranteed. And on most distro kernels the USB storage drivers are modules, so they need adding to the initramfs and loading before the script waits for the disk.
Caveats
Docker images aren't built to boot. Beyond the init system, they have no network configuration, no kernel modules, no root password, and are often stripped down to save space. They'll boot, but expect to fill in what a container normally gets from its host. Mixing kernels between distros works for the boot itself, but drivers and firmware follow the root filesystem, not the kernel.
So, can a Docker image boot a real machine?
Yes, as long as you give it three things it never had: a kernel, a way to find its disk, and a PID 1. The kernel and initrd do the pulling, and the root filesystem is just cargo.