Skip to content

Instantly share code, notes, and snippets.

@taylordaughtry
Created January 19, 2019 04:04
Show Gist options
  • Save taylordaughtry/7dbb560af87152b2904d0867391e76cc to your computer and use it in GitHub Desktop.
Save taylordaughtry/7dbb560af87152b2904d0867391e76cc to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Via https://medium.com/@sean.handley/how-to-set-up-docker-for-mac-with-native-nfs-145151458adc
OS=`uname -s`
if [ $OS != "Darwin" ]; then
echo "This script is OSX-only. Please do not run it on any other Unix."
exit 1
fi
if [[ $EUID -eq 0 ]]; then
echo "This script must NOT be run with sudo/root. Please re-run without sudo." 1>&2
exit 1
fi
echo ""
echo " +-----------------------------+"
echo " | Setup native NFS for Docker |"
echo " +-----------------------------+"
echo ""
echo "WARNING: This script will shut down running containers."
echo ""
echo -n "Do you wish to proceed? [y]: "
read decision
if [ "$decision" != "y" ]; then
echo "Exiting. No changes made."
exit 1
fi
echo ""
if ! docker ps > /dev/null 2>&1 ; then
echo "== Waiting for docker to start..."
fi
open -a Docker
while ! docker ps > /dev/null 2>&1 ; do sleep 2; done
echo "== Stopping running docker containers..."
docker-compose down > /dev/null 2>&1
docker volume prune -f > /dev/null
osascript -e 'quit app "Docker"'
echo "== Resetting folder permissions..."
U=`id -u`
G=`id -g`
sudo chown -R "$U":"$G" .
echo "== Setting up nfs..."
LINE="/Users -alldirs -mapall=$U:$G localhost"
FILE=/etc/exports
sudo cp /dev/null $FILE
grep -qF -- "$LINE" "$FILE" || sudo echo "$LINE" | sudo tee -a $FILE > /dev/null
LINE="nfs.server.mount.require_resv_port = 0"
FILE=/etc/nfs.conf
grep -qF -- "$LINE" "$FILE" || sudo echo "$LINE" | sudo tee -a $FILE > /dev/null
echo "== Restarting nfsd..."
sudo nfsd restart
echo "== Restarting docker..."
open -a Docker
while ! docker ps > /dev/null 2>&1 ; do sleep 2; done
echo ""
echo "SUCCESS! Now go run your containers 🐳"
@taylordaughtry
Copy link
Author

taylordaughtry commented Jan 23, 2020

If you've never setup NFS on your system, run the above script the very-first time you setup NFS.

Then, when you want to use an NFS volume on a project, do the following steps:

  1. Add PROJECT_DIRECTORY=[absolute/path/to/your/project/root] to your .env
  2. Add the following volume declaration to your project's docker-compose.yml file:
volumes:
  my-nfs-volume:
    driver: local
    driver_opts:
      type: nfs
      o: addr=host.docker.internal,rw,nolock,hard,nointr,nfsvers=3
      device: ":${PROJECT_DIRECTORY}"
  1. Rebuild the image via docker-compose build / docker-compose up --build / so forth.

Current issues

  1. The NFS volume will collide between projects, so currently you'll need to rebuild the image if you swap to another project using an NFS volume. (You can namespace the volume name to possibly solve this; haven't tested it yet.)

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