We propose here a small script to convert a docker image into a VM image (qemu), based on libguestfs tools for simplicity. This work is inspired by iximiuz's blog, also available on GitHub.
Given a docker container <docker>, our script ./docker2vm.sh makes an output
VM image <vm.img> (in raw format), that runs on qemu-system-x86_64. This
script requires root privilege.
Usage: ./docker2vm.sh <docker> <vm.img>
Disclaimer: It works only on Linux host for Linux guest. But, it probably can be easily adapted (or not) for other systems!
First, you need to install Docker and Qemu. Then, you need to fulfill these additional requirements for libguestfs or extlinux.
$ sudo apt install qemu-system-x86 qemu-utils
$ sudo apt install guestfs-tools extlinuxYou need to install in your docker container both a Linux kernel (/vmlinuz and
/initrd files) and systemd (/sbin/init). Update your Dockerfile with this command:
RUN apt install -yq linux-image-amd64 systemd-sysvWhile this step is not useful for the container itself, it will be useful to make a bootable VM image.
Besides, we provide a simple bootloader configuration for extlinux, that is
described in syslinux.cfg. Here, we append the kernel option
console=ttyS0 to enforce the system boot in text-mode.
Lets's consider a basic Dockerfile based on a Linux Debian 11.
# build docker (without build context)
$ docker build -t tmp/demo - < misc/Dockerfile.demo
# test docker
$ docker run -it tmp/demo
# make vm image
$ sudo ./docker2vm.sh tmp/demo demo.img
# change image owner
$ sudo chown $USER:$USER demo.img
# remove docker image (if needed)
$ docker image rm --force tmp/demo
# test vm with Qemu (the root password is "root")
$ qemu-system-x86_64 -enable-kvm -m 200M -hda demo.img -nographicFinally, you can convert this raw image in another format for Qemu (qcow2) or VirtualBox (vdi).
# convert in qcow2 format for Qemu
$ qemu-img convert -c demo.img -O qcow2 demo.qcow2
# convert in vdi format for VirtualBox
$ VBoxManage convertfromraw --format vdi demo.img demo.vdi