How to launch qemu-kvm from inside a Docker container?

int 2Eh picture int 2Eh · Jan 24, 2018 · Viewed 16.6k times · Source

Assuming the host system already supports KVM, is it possible to create a docker image which contains some scripts to launch a VM (inside the container) with virsh and QEMU-KVM?

We are looking into dockerize a script which launches a VM through QEMU-KVM and extracts some results from the VM.

Answer

docker --privileged

Some working commands from Ubuntu 17.10 host, Docker 1.13.1:

sudo docker run --name ub16 -i --privileged -t ubuntu:16.04 bash

Then inside Docker:

apt-get update -y
apt-get install qemu -y
qemu-system-x86_64
qemu-system-x86_64 \
  -append 'root=/dev/vda console=ttyS0' \
  -drive file='rootfs.ext2.qcow2,if=virtio,format=qcow2'  \
  -enable-kvm \
  -kernel 'bzImage' \
  -nographic \
;

Root file system and bzImage generated with this setup.