Skip to content

Docker with NFS on macOS

Fillip Hannisdal edited this page Nov 14, 2018 · 1 revision

Setup NFS

Create & run this shell script, take care to change /www/public_html to the folder containing your web root.

#!/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 ""

if docker ps > /dev/null 2>&1 ; then
	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 "== Stopping running docker containers..."
	docker-compose down > /dev/null 2>&1
	docker volume prune -f > /dev/null

	osascript -e 'quit app "Docker"'
fi	

echo "== Setting up nfs..."
LINE="/www/public_html -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 🐳"

composer.yml

version: '2'
services:
  app:
    image: "belazor/dbtech-devel:php-apache-${DOCKER_PHP_VERSION}"
    container_name: "php${DOCKER_PHP_VERSION}"
    ports:
        - "${DOCKER_PHP_PORT}:80"
    volumes:
      - "nfsmount:/var/www/html"

volumes:
  nfsmount:
    driver: local
    driver_opts:
      type: nfs
      o: addr=host.docker.internal,rw,nolock,hard,nointr,nfsvers=3
      device: ":/www/public_html" # Change this to wherever your webroot is on your local drive
Clone this wiki locally