Skip to content

Instantly share code, notes, and snippets.

@mattes
Last active December 3, 2019 20:26
Show Gist options
  • Save mattes/2d0ffd027cb16571895c to your computer and use it in GitHub Desktop.
Save mattes/2d0ffd027cb16571895c to your computer and use it in GitHub Desktop.

Build boot2docker.iso with VirtualBox Guest Additions

Allows me to mount /Users into boot2docker which is incredibly useful for my local Docker environment under Mac OSX. So from Mac I can do sth like this:
docker run -v /Users/mattes/somedir:/data/somedir [..].

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.

I tried to make the mount permanent from within the VirtualBox GUI (see screenshot) but that didn't work. So I added the mount logic to $ROOTFS/etc/rc.d/vbox-guest-additions-permanent-mount (see Dockerfile.tmpl)

See build log here https://gist.github.com/mattes/6bed15318e93925b1280

# generate Dockerfile from Dockerfile.tmpl
chmod +x build_docker.sh
./build_docker.sh

# build the actual boot2docker.iso with virtual box guest additions
docker build -t mattes/boot2docker-vbga .

# the following line is proposed in many tutorials, but does not work for me
# (it outputs an iso that won't work)
docker run -i -t --rm mattes/boot2docker-vbga > boot2docker.iso

# so I do:
docker run -i -t --rm mattes/boot2docker-vbga /bin/bash
# then in a second shell:
docker cp <Container-ID>:boot2docker.iso boot2docker.iso

# use the new boot2docker.iso
boot2docker stop
mv ~/.boot2docker/boot2docker.iso ~/.boot2docker/boot2docker.iso.backup
mv boot2docker.iso ~/.boot2docker/boot2docker.iso
VBoxManage sharedfolder add boot2docker-vm -name home -hostpath /Users
boot2docker up
boot2docker ssh "ls /Users" # to verify if it worked
#!/usr/bin/env sh
get_vbox_version(){
local VER
VER=$(VBoxManage -v | awk -F "r" '{print $1}')
if [ -z "$VER" ]; then
echo "ERROR"
else
echo "$VER"
fi
}
write_vbox_dockerfile(){
local VER
VER=$(get_vbox_version)
if [ ! "$LATEST_RELEASE" = "ERROR" ]; then
sed "s/\$VBOX_VERSION/$VER/g" Dockerfile.tmpl > Dockerfile
else
echo "WUH WOH"
fi
}
write_vbox_dockerfile
# using VirtualBox version $VBOX_VERSION
FROM boot2docker/boot2docker
RUN apt-get install 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 /Users && sudo mount -t vboxsf home /Users' >> $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"]
@toby-griffiths
Copy link

Thanks for this info. I have a slight issue in that I'm attempting to mount a different directory, however editing the /etc/rc.d/vbox-guest-additions-permanent-mount file, saving & restarting the box, causes the changes to this file to get lost. Do you know why this might be?

@mattes
Copy link
Author

mattes commented Jul 7, 2014

Sorry, I overlooked your comment. I don't think saving & restarting works in this case. You would need to build a complete new .iso with the instructions above.

@mitar
Copy link

mitar commented Jul 12, 2014

Have you read here: https://www.virtualbox.org/manual/ch04.html#sf_mount_auto

It seems automatic mounting works a bit differently on Linux, than manual mounting.

@mitar
Copy link

mitar commented Jul 13, 2014

I discovered an issue with using VirtualBox Guest Additions and nginx. I am using volumes to map my Wordpress blog into the container so that I can develop easily. And I use nginx to server PHP and static files. I discovered that for static files if I change them outside the container, nginx still servers the old file. File inside the container changes, only nginx servers the old version. After some debugging I discovered that sendfile option was the reason. It was turned on and it seems it does not work well together with all this VirtualBox sharing and volume mapping combination. After I disabled sendfile nginx servers the latest version of the file.

@razorgirl
Copy link

Thanks for the info! It made using fig possible for me.

BTW: when using the command docker run -t --rm mattes/boot2docker-vbga cat boot2docker.iso > boot2docker.iso, a correct ISO should be written.

@xiaods
Copy link

xiaods commented Aug 27, 2014

How about move to github repo, let everyone can sharing the script.

Copy link

ghost commented Sep 8, 2014

This is amazing, thank you for sharing the instructions! Would be nice if moved it to a repo so you can get contributors.

@jamonation
Copy link

What about doing this on Windows? How would that work given /Users does not exist on that OS.

@ryanobjc
Copy link

very nice precisely what i need!

@steeef
Copy link

steeef commented Sep 25, 2014

@mattes this is awesome, thank you. Worked like a charm.

@cirpo
Copy link

cirpo commented Sep 27, 2014

I downloaded your iso here http://static.dockerfiles.io/boot2docker-v1.2.0-virtualbox-guest-additions-v4.3.14.iso and I can see that /Users is correctly mounted but my code is on another partition (/Volumes/Sensitive/code).

I tried to mount it with VBoxManage sharedfolder add boot2docker-vm -name code -hostpath /Volumes/Senitive/code but when I run boot2docker ssh I can't find that dir.

What should I do? Create another boot2docker iso with your script and include my shared folder?

Am I missing anything? Thanks a lot.

@cirpo
Copy link

cirpo commented Sep 27, 2014

@mattes and it worked! Thanks!

@chris-snider
Copy link

I just tried this today on a Windows 7 box. I followed the instructions at https://medium.com/boot2docker-lightweight-linux-for-docker/boot2docker-together-with-virtualbox-guest-additions-da1e3ab2465c starting with the "...system replace the old one at ~/.boot2docker/boot2docker.iso". (I downloaded the ISO instead of building my own from http://static.dockerfiles.io/boot2docker-v1.2.0-virtualbox-guest-additions-v4.3.14.iso).

After running the boot2docker init to generate the new virtual machine, I ran the same vboxmanage to add another shared folder from a separate partition:

using cygwin bash shell

ensure the boot2docker VM is not running

boot2docker down

add the expected users partition for Windows system

VBoxManage sharedfolder add boot2docker-vm -name home -hostpath c:/Users

add my other partition

VBoxManage sharedfolder add boot2docker-vm -name -hostpath

example

VBoxManage sharedfolder add boot2docker-vm -name dockshare -hostpath f:/docker/

restart the boot2docker VM

boot2docker up

log in

boot2docker ssh

verify original users mapped

ls -alF /Users

the fun stuff, adding the second path in NON-PERSISTENT mode

sudo modprobe vboxsf && sudo mkdir / && sudo mount -t vboxsf /

example

sudo modprobe vboxsf && sudo mkdir /hostshare && sudo mount -t vboxsf dockshare /hostshare

NOTE:
To make a persistent share, you have to modify the dockerfile.tmpl shown above adding the additional shares to this section:

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 /Users && sudo mount -t vboxsf home /Users' >> $ROOTFS/etc/rc.d/vbox-guest-additions-permanent-mount;
echo 'sudo modprobe vboxsf && sudo mkdir / && sudo mount -t vboxsf /' >> $ROOTFS/etc/rc.d/vbox-guest-additions-permanent-mount

Rerun the build script to create the ISO file and run the steps again starting with the replace current ISO after stopping the B2D VM

@mcqueenorama
Copy link

It works for me! I had to add the -y option to the 7z command for some reason "7z x -y ...", and then to recover from the error had to remove the mkdir commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment