When you are using Docker at Mac for development, you may be noticed, that the speed of the attached volume is horrifically slow. To cut a long story short, it is due to the way a Docker virtual machine works on a Mac.
The best way to speed up the attached Docker volume at Mac, for now, is to use NFS.
It is very simple, you should edit two files: /etc/nfs.conf and /etc/exports.
sudo nano /etc/nfs.conf
By default, it contains:
#
# nfs.conf: the NFS configuration file
#
Add a string there:
nfs.server.mount.require_resv_port = 0
Then edit the /etc/exports file, by default it is empty.
sudo nano /etc/exports
Add there:
<path to your dir for NFS sharing> -alldirs -mapall=501:20 localhost
Here the “path to your dir for NFS sharing” is just a path to some folder you are using for development and where the contents of your volume are located.
Next, just restart the NFS service:
sudo nfsd restart
Then make some modifications to your docker-compose.yml:
version: '2'
services:
magento:
...
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: ":<absolute path to your data to mount>"
I have measured the speed of the attached volume with and without NFS:
# Command to run test
dd if=/dev/zero of=file.txt count=512000 bs=1024 && rm file.txt
# Without NFS, cached
512000+0 records in
512000+0 records out
524288000 bytes (524 MB, 500 MiB) copied, 218.471 s, 2.4 MB/s
# With NFS
512000+0 records in
512000+0 records out
524288000 bytes (524 MB, 500 MiB) copied, 7.61804 s, 68.8 MB/s
As you can see, with NFS you gain 28X mounted-storage performance using Docker on Mac.