18

I've been looking at Packer.io, and would love to use it to provision/prepare the vagrant (VirtualBox) boxes used by our developers.

I know I could build the boxes with VirtualBox using the VirtualBox Packer builder, but find the layer stacking of Docker to provide a much faster development process of the boxes.

How do I produce the image with a Dockerfile and then export it as a Vagrant box?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Fredrik Wendt
  • 988
  • 1
  • 10
  • 17

3 Answers3

24

Find the size of the docker image from docker images

REPOSITORY   TAG    IMAGE ID       CREATED             SIZE
mybuntu   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=2G
mkfs.ext2 -F mybuntu.img
sudo mount -o loop mybuntu.img /mnt
docker export <CONTAINER-ID> | sudo tar x -C /mnt
sudo umount /mnt

Use qemu-utils to convert to vmdk

sudo apt-get install qemu-utils
qemu-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

Chenna V
  • 10,185
  • 11
  • 77
  • 104
8

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"
  end
end 

Once your image is built, you can produce a vagrant box using the vagrant package command.

Emyl
  • 10,460
  • 2
  • 35
  • 34
  • 1
    Yes, I'm very aware of this and the point of this question was to NOT use Packer's vagrant Builder, which is exactly what you're proposing. – Fredrik Wendt May 03 '14 at 14:30
1

This is the route I'm going to try:

This will allow me to setup/provision the machine using Docker, and then run it in Virtualbox controlled via vagrant.

Community
  • 1
  • 1
Fredrik Wendt
  • 988
  • 1
  • 10
  • 17
  • 1
    Turned out that Docker version 0.10.0, build dc9c28f together with Ubuntu 12.10 is really unstable - it has frozen one of my servers multiple times: it has caused kernel panics. I've paused this attempt for now, but in theory there's no reason why this approach shouldn't work. – Fredrik Wendt Jun 02 '14 at 06:14