Skip to content

Instantly share code, notes, and snippets.

@olalonde
Created August 13, 2015 14:48
Show Gist options
  • Save olalonde/74de9d053448b977f77e to your computer and use it in GitHub Desktop.
Save olalonde/74de9d053448b977f77e to your computer and use it in GitHub Desktop.
Use NFS instead of vboxsf in Docker Machine
#!/bin/bash
#
# This script will mount /Users in the boot2docker VM using NFS (instead of the
# default vboxsf). It's probably not a good idea to run it while there are
# Docker containers running in boot2docker.
#
# Usage: sudo ./boot2docker-use-nfs.sh
#
docker-machine ssh default << EOF | grep "already mounted" &> /dev/null
mount | grep nfs > /dev/null && \
echo "/Users already mounted with NFS" && \
exit
EOF
if [ "$?" == "0" ]
then
# Already mounted
exit
fi
if [ "$USER" != "root" ]
then
echo "This script must be run with sudo: sudo ${0}"
exit -1
fi
# Run command as non root http://stackoverflow.com/a/10220200/96855
B2D_IP=$(sudo -u ${SUDO_USER} docker-machine ip default)
if [ "$B2D_IP" == "" ]
then
sudo -u ${SUDO_USER} docker-machine start default
eval "$(sudo -u ${SUDO_USER} docker-machine env default)"
B2D_IP=$(sudo -u ${SUDO_USER} docker-machine ip default &> /dev/null)
#echo "You need to start boot2docker first: boot2docker up && \$(boot2docker shellinit) "
#exit -1
fi
OSX_IP=$(ifconfig en0 | grep --word-regexp inet | awk '{print $2}')
MAP_USER=${SUDO_USER}
MAP_GROUP=$(sudo -u ${SUDO_USER} id -n -g)
RESTART_NFSD=0
EXPORTS_LINE="/Users -mapall=${MAP_USER}:${MAP_GROUP} ${OSX_IP}"
grep "$EXPORTS_LINE" /etc/exports > /dev/null
if [ "$?" != "0" ]
then
RESTART_NFSD=1
# Backup exports file
$(cp -n /etc/exports /etc/exports.bak) && \
echo "Backed up /etc/exports to /etc/exports.bak"
# Delete previously generated line if it exists
grep -v '^/Users ' /etc/exports > /etc/exports
# We are using the OS X IP because the b2d VM is behind NAT
echo "$EXPORTS_LINE" >> /etc/exports
fi
NFSD_LINE="nfs.server.mount.require_resv_port = 0"
grep "$NFSD_LINE" /etc/nfs.conf > /dev/null
if [ "$?" != "0" ]
then
RESTART_NFSD=1
# Backup /etc/nfs.conf file
$(cp -n /etc/nfs.conf /etc/nfs.conf.bak) && \
echo "Backed up /etc/nfs.conf to /etc/nfs.conf.bak"
echo "nfs.server.mount.require_resv_port = 0" >> /etc/nfs.conf
fi
if [ "$RESTART_NFSD" == "1" ]
then
echo "Restarting nfsd"
nfsd update 2> /dev/null || (nfsd start && sleep 5)
fi
sudo -u ${SUDO_USER} docker-machine ssh default << EOF
echo "Unmounting /Users"
sudo umount /Users 2> /dev/null
echo "Starting nfs-client"
sudo /usr/local/etc/init.d/nfs-client start 2> /dev/null
echo "Mounting /Users"
sudo mount $OSX_IP:/Users /Users -o rw,async,noatime,rsize=32768,wsize=32768,proto=tcp,nfsvers=3
echo "Mounted /Users:"
ls -al /Users
exit
EOF
@vortec
Copy link

vortec commented Jan 20, 2016

Hi, thanks for this! It works great, but I'm getting an error message right at the beginning:

"machine does not exist"

I think as root it won't find the VBox that was started as my local user?

@ekkis
Copy link

ekkis commented Jul 3, 2016

here was my experience:

$ sudo docker-machine-use-nfs.sh

Password:
machine does not exist
grep: /etc/exports: No such file or directory
cp: /etc/exports: No such file or directory
Backed up /etc/nfs.conf to /etc/nfs.conf.bak
Restarting nfs
Starting the nfs service
Boot2Docker version 1.12.0-rc2, build HEAD : 52952ef - Fri Jun 17 21:01:09 UTC 2016
Docker version 1.12.0-rc2, build 906eacd
Unmounting /Users
Starting nfs-client
Starting nfs client utilities.
Mounting /Users
mount: RPC: Remote system error - Connection refused
mount: mounting 192.168.1.67:/Users on /Users failed: Bad file descriptor
Mounted /Users:
total 0
drwxr-xr-x 2 root root 40 Jul 3 01:10 ./
drwxr-xr-x 17 tc staff 420 Jul 3 01:10 ../

@ekkis
Copy link

ekkis commented Jul 3, 2016

and a little more info on it:

$ sudo bash -x docker-machine-use-nfs.sh

  • docker-machine ssh default
  • grep 'already mounted'
    machine does not exist
  • '[' 1 == 0 ']'
  • '[' root '!=' root ']'
    ++ sudo -u ekkis docker-machine ip default
  • B2D_IP=192.168.99.100
  • '[' 192.168.99.100 == '' ']'
    ++ ifconfig en0
    ++ grep --word-regexp init
    ++ awk '{print $2}'
  • OSX_IP=192.168.1.67
  • MAP_USER=ekkis
    ++ sudo -u ekkis id -n -g
  • MAP_GROUP=staff
  • RESTART_NFSD=0
  • EXPORTS_LINE='/Users -mapall=ekkis:staff 192.168.1.67'
  • grep '/Users -mapall=ekkis:staff 192.168.1.67' /etc/exports
  • '[' 0 '!=' 0 ']'
  • NFSD_LINE='nfs.server.mount.require_resv_port = 0'
  • grep 'nfs.server.mount.require_resv_port = 0' /etc/nfs.conf
  • '[' 0 '!=' 0 ']'
  • '[' 0 == 1 ']'

interestingly, I can do docker-machine ssh default and get into the machine... thoughts?

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