Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How To Set Up Docker For Mac with Native NFS
version: '2'
services:
api:
volumes:
- "nfsmount:${CONTAINER_DIR}"
volumes:
nfsmount:
driver: local
driver_opts:
type: nfs
o: addr=host.docker.internal,rw,nolock,hard,nointr,nfsvers=3
device: ":${SOURCE_DIR}"
export CONTAINER_DIR=/myapp
export SOURCE_DIR=/Users/me/myapp
#!/usr/bin/env bash
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 🐳"
@blackandred

This comment has been minimized.

Copy link

commented Apr 30, 2018

Thank you.

@michalkleiner

This comment has been minimized.

Copy link

commented May 16, 2018

Would sudo touch $FILE be better than sudo cp /dev/null $FILE (line 55) for cases when the file exists? For example, I already have the file with entries for other NFS shares using vagrant for other projects, so I don't want it emptied.

Otherwise thanks a lot for putting this together!

@rivetmichael

This comment has been minimized.

Copy link

commented May 24, 2018

Thank you very effective !

@codename-

This comment has been minimized.

Copy link

commented Sep 18, 2018

there's no need for sudo in sudo echo "$LINE" :)

@talya

This comment has been minimized.

Copy link

commented Oct 8, 2018

Thanks, super useful!

Any chance you remember how you managed to get it working with nfsv4?
I get this error: “protocol not supported” when using ‘nfsvers=4’, ‘nfsvers=4.1’ and ‘vers=4’

Also, what seemed more stable with nfsv3? I’m actually trying to move to v4 to see if it improves performance, mainly around deleting many files and also how nfsv3 implements the “silly rename” (causing “”Device or resource busy” errors on random .nfsXXXXX’ files)

@idflood

This comment has been minimized.

Copy link

commented Nov 12, 2018

Just wondering why on line 50 you do sudo chown -R "$U":"$G" .?

It will change the permission of the current directory and subdirectories where this script is called if I'm right. So the script should probably be executed at the root of you app/shared folder, maybe a small comment could help : )

@wss-chadical

This comment has been minimized.

Copy link

commented Feb 19, 2019

heads up: OSX Mojave has some new security restrictions. The script as is runs, but does not actually create the /etc/exports due to an unsurfaced permissions error.

Quick fix here:
https://www.cubewebsites.com/blog/software/quick-fix-macos-mojave-vagrant-error-tee-etc-exports-operation-not-permitted/

You can quickly fix this issue by following the following steps:
Open “System Preferences”
Go to “Security & Privacy”
Then Privacy Tab
Click the “padlock” icon in the bottom-left corner where it says “Click the lock to make changes” – enter your password when prompted
On the list of permissions on the left, select “Full Disk Access”
Now, on the right-hand side, click the little “+” icon
This will open a browse window, use this to select your preferred Terminal e.g. “Applications > iTerm” or “Applications > Utilities > Terminal”
If the selected Terminal is already open, you’ll be asked to Quit it – quit the application and restart it
When you restart the selected application, you may prompted if you’d like to allow the app to make changes to your computer – accept and enter your password if required
Try mounting your vagrant machine and now the above error should no longer appear!
@lhridley

This comment has been minimized.

Copy link

commented Mar 9, 2019

@wss-chadical The blog post in your comment above is protected with basic auth.

@maclordaj

This comment has been minimized.

Copy link

commented May 26, 2019

in my case the file /etc/nfs.conf already existed with other options on my system and it seems the script added the nfs.server.mount.require_resv_port = 0 at the end of the file but without a new line. Took me a while to notice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.