Skip to content

Instantly share code, notes, and snippets.

@NathanZook
Created August 29, 2014 18:48
Show Gist options
  • Save NathanZook/d04fecf1784611c7eb30 to your computer and use it in GitHub Desktop.
Save NathanZook/d04fecf1784611c7eb30 to your computer and use it in GitHub Desktop.

Build boot2docker.iso with VirtualBox Guest Additions

Allows you to mount an local folder into boot2docker which is incredibly useful for local Docker environment under Mac OSX. So from Mac you do stuff like this:
docker run -v /Users/william/repos/reallysickcode:/code [..].

This Dockerfile will download the latest boot2docker image (see FROM boot2docker/boot2docker) and adds VirtualBox Guest Additions for your running VirtualBox version.

See also boot2docker/boot2docker#284.

The mount logic is handled with $ROOTFS/etc/rc.d/vbox-guest-additions-permanent-mount (see Dockerfile.tmpl)

# generate Dockerfile from Dockerfile.tmpl and build it
chmod +x build_docker.sh
./build_docker.sh [full_path_to_mount]
#!/bin/bash
if [[ $# -ne 1 ]]; then
echo "Usage: build_docker.sh [full_path_to_mount]"
exit 1
fi
mount_path=$1
vbox_version=`VBoxManage --version | sed "s/r.*//"`
sed "s/\$VBOX_VERSION/$vbox_version/g" Dockerfile.tmpl > Dockerfile.versioned.tmpl
sed "s|\$MOUNT_PATH|$mount_path|g" Dockerfile.versioned.tmpl > Dockerfile
rm Dockerfile.versioned.tmpl
# build the actual boot2docker.iso with virtual box guest additions and copy the iso to the local filesystem
docker build -t boot2docker-vbga .
id=`docker run -dit boot2docker-vbga /bin/bash`
docker cp $id:/boot2docker.iso .
docker stop $id
docker rm $id
# replace the original iso
boot2docker stop
mv ~/.boot2docker/boot2docker.iso ~/.boot2docker/boot2docker.iso.backup
mv boot2docker.iso ~/.boot2docker/boot2docker.iso
# Make the folder available to VBox
VBoxManage sharedfolder add boot2docker-vm -name localmount -hostpath $mount_path
# Bring up the VM and do a sanity check
boot2docker up
boot2docker ssh "ls $mount_path"
exit $?
# using VirtualBox version $VBOX_VERSION
FROM boot2docker/boot2docker
RUN apt-get install -y p7zip-full
RUN mkdir -p /vboxguest && \
cd /vboxguest && \
curl -L -o vboxguest.iso http://download.virtualbox.org/virtualbox/$VBOX_VERSION/VBoxGuestAdditions_$VBOX_VERSION.iso && \
7z x vboxguest.iso -ir'!VBoxLinuxAdditions.run' && \
sh VBoxLinuxAdditions.run --noexec --target . && \
mkdir x86 && cd x86 && tar xvjf ../VBoxGuestAdditions-x86.tar.bz2 && cd .. && \
mkdir amd64 && cd amd64 && tar xvjf ../VBoxGuestAdditions-amd64.tar.bz2 && cd .. && \
cd amd64/src/vboxguest-$VBOX_VERSION && KERN_DIR=/linux-kernel/ make && cd ../../.. && \
cp amd64/src/vboxguest-$VBOX_VERSION/*.ko $ROOTFS/lib/modules/$KERNEL_VERSION-tinycore64 && \
mkdir -p $ROOTFS/sbin && cp x86/lib/VBoxGuestAdditions/mount.vboxsf $ROOTFS/sbin/
RUN echo "" >> $ROOTFS/etc/motd; \
echo " boot2docker with VirtualBox guest additions version $VBOX_VERSION" >> $ROOTFS/etc/motd; \
echo "" >> $ROOTFS/etc/motd
# make mount permanent @todo it works, but its ugly. where should this go?
RUN echo '#!/bin/sh' >> $ROOTFS/etc/rc.d/vbox-guest-additions-permanent-mount; \
echo 'sudo modprobe vboxsf && sudo mkdir -p $MOUNT_PATH && sudo mount -t vboxsf localmount $MOUNT_PATH' >> $ROOTFS/etc/rc.d/vbox-guest-additions-permanent-mount
RUN chmod +x $ROOTFS/etc/rc.d/vbox-guest-additions-permanent-mount
RUN echo '/etc/rc.d/vbox-guest-additions-permanent-mount' >> $ROOTFS/opt/bootsync.sh
RUN depmod -a -b $ROOTFS $KERNEL_VERSION-tinycore64
RUN /make_iso.sh
CMD ["cat", "boot2docker.iso"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment