Cloud Native 17 min read

Turn Docker Images into a Full Desktop OS: A Step‑by‑Step Guide

This tutorial explains how to use Docker images as a portable desktop operating system by building a custom image, configuring mkinitcpio‑docker‑hooks, preparing the kernel and initramfs, and setting up the boot manager to launch the system directly from the Docker layer.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Turn Docker Images into a Full Desktop OS: A Step‑by‑Step Guide

The author explores using Docker images as a complete desktop system, highlighting Docker's ability to package applications and their environments for easy migration across multiple computers.

Docker Philosophy

By writing a Dockerfile you can install and configure all required libraries and tools, then build the image with docker build, push it to DockerHub with docker push, and retrieve it on any machine using docker pull. This ensures identical environments on all devices.

Docker Storage Driver

Docker creates a new layer for each instruction in the Dockerfile. When pulling or pushing images, only the incremental layers are transferred. The underlying storage driver (graph driver) such as aufs, overlay, btrfs, zfs, or devicemapper implements copy‑on‑write, allowing layers to be combined without duplicating data.

Booting Directly into a Docker Image

Linux boots by loading an initramfs, which extracts its contents to the root filesystem ( /) and runs /init. After early initialization, the real root is mounted based on kernel parameters ( root, rootflags) and switch_root hands control to the final init process. By adding a hook to the initramfs, a snapshot of a Docker image can be created as the writable layer and then mounted as the real root, allowing the Docker image to run as the host OS.

Benefits of This Approach

The entire system resides in the cloud; the local machine only holds a cache, eliminating manual backups.

The Dockerfile serves as a clear, version‑controlled record of the system configuration.

Each boot starts from a fresh, clean system, avoiding leftover junk or corrupted data.

Deploying a new machine is as simple as pulling the image from DockerHub.

System updates are performed by rebuilding the image from the latest packages, avoiding file‑conflict or dependency issues.

mkinitcpio‑docker‑hooks Usage

Ensure /var/lib/docker resides on a Btrfs subvolume.

Prepare a Docker image suitable for bare‑metal boot (install the base group and any required packages).

Install and configure mkinitcpio-docker-hooks inside the image.

Build the kernel and initramfs within the container.

Prepare a “top layer” directory for host‑specific configuration files.

Configure the boot manager to launch the system from the Docker image.

Preparing the Docker Image

FROM zasdfgbnm/archlinux-yaourt
USER root
RUN pacman -Syu --noconfirm base

Installing and Configuring mkinitcpio‑docker‑hooks

RUN sudo -u user yaourt -S --noconfirm mkinitcpio-docker-hooks
{
  "docker_image": "archlinux/base",
  "docker_tag": "latest"
}
RUN sed -i 's/archlinux/base/sample_image/g' /etc/docker-btrfs.json
RUN perl -i -pe 's/(?<=^HOOKS=()(.*)(?=())/$1 docker-btrfs/g' /etc/mkinitcpio.conf

Preparing Kernel and Initramfs

docker run -v $(pwd):/workspace -w /workspace -it sample_image bash
mkinitcpio -p linux
cp /boot/* .
exit

Preparing the Top Layer

Create a directory that mirrors the paths of host‑specific files (e.g., etc/fstab, etc/X11/xorg.conf) and copy the configured files from the running container into this directory.

Configuring the Boot Manager (example with rEFInd)

menuentry archlinux-docker {
    icon EFI/refind/icons/os_arch.png
    volume linux
    loader boot_docker/vmlinuz-linux
    initrd boot_docker/initramfs-linux.img
    options "root=LABEL=linux rootflags=subvol=docker_rwlayer rw docker_path=docker toplayer=LABEL=linux toplayer_path=boot_docker"
}

After setting the kernel parameters to point to the Docker subvolume and the top‑layer configuration, reboot the machine and the system will boot directly from the Docker image.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

DockerLinuxContainerDesktop OSBtrfsmkinitcpio
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.