Slow docker on Mac? Use NFS!

Hey guys, how are you? Well, this is a very quick post about a problem I had been having with Docker on my Mac for a long time and which I FINALLY managed to solve after a long search on the internet.

The Problem

As many here already know, I’m a web developer and I work mainly with PHP projects and their more known frameworks. For some time I was having serious problems with slowness both when loading the containers and also opening the projects in the browser. Friends who used it on Linux and Windows said it didn’t have this bottleneck and yes, I know that on Linux the stop runs smoother because Docker runs almost native there.

Solution

Browsing the internet I saw that many people had this problem and everyone, almost unanimously, indicated using a file system called NFS (Network File System) when mapping their volumes. The “default file system” that Docker uses is quite slow in the Mac environment, for that reason, many dev’s used NFS.

Want to know how to configure? So, come with me!

Vem Nenem GIF by MTV Brasil

Configuring the nfsd service

The first step is to know if the nfsd service is enabled and if it is running on your Mac. To do this, open your terminal and type:

$ sudo nfsd status

nfsd service is enabled
nfsd is running (pid 60994, 8 threads)

If your service has not enabled, just run the command:

$ sudo nfsd enable

Before starting the nfsd service, you must first configure the /etc/exports file. If it doesn’t exist, which was my case, create it and put:

$ sudo nano /etc/exports

# For MacOS Catalina Users
/System/Volumes/Data -alldirs -mapall=501:20 localhost

# For Others MacOS Versions
/Users -alldirs -mapall=501:20 localhost

In summary, this command allows the sharing of any Home directory. It is very likely that you have to give permission to save this file.

That done, just start the nfsd service, as shown below:

$ sudo nfsd start

Starting the nfsd service

Now we are going to add an instruction in /etc/nfs.conf. Open it and add this:

$ sudo nano /etc/nfs.conf

nfs.server.mount.require_resv_port = 0

This is to tell the NFS daemon to allow connections from any port. This is necessary as Docker NFS connections can be blocked.

Now let’s go to docker-compose.yml

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:

version: "3"
services:
  php:
    image: php
    container_name: "your-container-name"
    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: ":/System/Volumes/Data/${PWD}"

In highlight 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.

I hope this technique helps you as it helped me. And if I said some nonsense or you have more tips, give an improvement in Docker in MacOS environments, please do not forget to put in the comments.

See ya.

References