Skip to content

orel33/docker2vm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Make a VM Image from a Docker Image

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.

Usage

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!

Requirements for Host

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 extlinux

Requirements for Guest

You 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-sysv

While 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.

Demo

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 -nographic

Finally, 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

aurelien.esnard@u-bordeaux.fr

About

Make a VM Image from a Docker Image

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages