macOS Docker use NFS Volume to get a better performance
macOS Docker NFS Volume
Why volumes on macOS work so slow? Docker For Mac runs the docker server within the virtual machine. The host machine shares the file system with the VM using osxfs. According to the documentation:
There are a number of issues with the performance of directories bind-mounted with osxfs. In particular, writes of small blocks, and traversals of large directories are currently slow. Additionally, containers that perform large numbers of directory operations, such as repeated scans of large directory trees, may suffer from poor performance.
My blog project based on Hexo(with next theme) consist of lots of files. Docker needs to keep in sync tons of files between the host machine and the container.
NFS Volume
The docker-compose allows defining the NFS volume that can be mounted to the container. The only thing you need to do is to configure and expose your NFS server.
If you want to mount volumes from NFS, you need to expose selected directories in /etc/export file.
First edit the /etc/exports file. If it doesn’t exist, which was my case, create it and eidt:
1 | /etc/exports |
Edit /etc/nfs.conf to tell the NFS daemon to allow connections from any port. This is necessary as Docker NFS connections can be blocked.
1 | /etc/nfs.conf |
Restart NFS to take .
1 | sudo nfsd restart |
Check NFS service status.
1 | sudo nfsd status |
Test
Now we are going to configure your docker-compose.yml so that the volumes are made via NFS. Below is a simple example that can be easily adapted to the project you are working on:
1 | version: '3' |
You see we have a volume called nfsmount, which can be called anything you want, which has some extra parameters for your volume.
That done, you can now upload your containers and see the huge difference this one will bring to you.
1 | docker-compose run app bash |