How can I convert a Docker image into a (vagrant) VirtualBox box?
Find the size of the docker image from docker images
REPOSITORY TAG IMAGE ID CREATED SIZEmybuntu 1.01 7c142857o35 2 weeks ago 1.94 GB
Run a container based on the image docker run mybuntu:1.01
Create a QEMU image from the container, Also, use the size of the image in the first command (seek=IMAGE_SIZE
). And, for the docker export
command retrieve the appropriate container id from docker ps -a
dd if=/dev/zero of=mybuntu.img bs=1 count=0 seek=2Gmkfs.ext2 -F mybuntu.imgsudo mount -o loop mybuntu.img /mntdocker export <CONTAINER-ID> | sudo tar x -C /mntsudo umount /mnt
Use qemu-utils
to convert to vmdk
sudo apt-get install qemu-utilsqemu-img convert -f raw -O vmdk mybuntu.img mybuntu.vmdk
More info on formats that are available for conversion can be found here. Now you can import the vmdk file in virtualbox
Provided that your target is VirtualBox, it could be probably better if you use Vagrant for the whole process.
Vagrant ships with a docker provisioner that could automatically install docker on the vm and build a Dockerfile
:
Vagrant.configure("2") do |config| config.vm.provision "docker" do |d| d.build_image "/vagrant/app" endend
Once your image is built, you can produce a vagrant box using the vagrant package
command.
This is the route I'm going to try:
- docker export to get a tar ball,
- then create a VMDK using the qemu-* tools and steps as outlined here: https://superuser.com/a/482127/59809
This will allow me to setup/provision the machine using Docker, and then run it in Virtualbox controlled via vagrant.