Skip to content

Instantly share code, notes, and snippets.

@johananl
Last active October 17, 2018 16:40
Show Gist options
  • Save johananl/95bff00f9d1b2f20d7888b70186caa56 to your computer and use it in GitHub Desktop.
Save johananl/95bff00f9d1b2f20d7888b70186caa56 to your computer and use it in GitHub Desktop.
KVM + Vagrant in Docker
docker build -t kvm-in-docker .
docker run -d --privileged kvm-in-docker
docker exec -it <id> bash
vagrant init <box>
vagrant up
vagrant ssh
# This is based on a Redhat blogpost:
# https://www.projectatomic.io/blog/2014/10/libvirtd_in_containers/
# https://github.com/projectatomic/docker-image-examples/blob/master/rhel-libvirt/libvirtd/Dockerfile
FROM centos:7
ENV VAGRANT_VERSION=2.2.0
RUN yum -y install systemd libvirt-daemon-driver-* libvirt-daemon libvirt-daemon-kvm libvirt-devel qemu-kvm gcc make wget openssh-clients && yum clean all; \
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*; \
systemctl enable libvirtd; \
systemctl enable virtlockd
#EXPOSE 16509
RUN echo "listen_tls = 0" >> /etc/libvirt/libvirtd.conf; \
echo 'listen_tcp = 1' >> /etc/libvirt/libvirtd.conf; \
echo 'tls_port = "16514"' >> /etc/libvirt/libvirtd.conf; \
echo 'tcp_port = "16509"' >> /etc/libvirt/libvirtd.conf; \
echo 'auth_tcp = "none"' >> /etc/libvirt/libvirtd.conf
RUN echo 'LIBVIRTD_ARGS="--listen"' >> /etc/sysconfig/libvirtd
RUN mkdir -p /var/lib/libvirt/images/
# Edit the service file which includes ExecStartPost to chmod /dev/kvm
RUN sed -i "/Service/a ExecStartPost=\/bin\/chmod 666 /dev/kvm" /usr/lib/systemd/system/libvirtd.service
# Install Vagrant
RUN wget https://releases.hashicorp.com/vagrant/${VAGRANT_VERSION}/vagrant_${VAGRANT_VERSION}_x86_64.rpm && \
rpm -i vagrant_${VAGRANT_VERSION}_x86_64.rpm && \
rm vagrant_${VAGRANT_VERSION}_x86_64.rpm
RUN vagrant plugin install vagrant-libvirt
VOLUME [ "/sys/fs/cgroup" ]
CMD ["/usr/sbin/init"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment