Improve Docker performance on macOS with Docker Machine

Improve Docker performance on macOS with Docker Machine

Recently I spoke a few fellow developers and they were unhappy with the performance of Docker on macOS. Personally I couldn't really relate to these performance issues. So what was going on? Well it wasn't hard to find the difference: They were using Docker for Mac, while I was using Docker Machine with Docker Machine NFS.

So yesterday I gave it a try. I send a colleague instructions on how to setup his development environment with Docker Machine. His reaction?

My docker is quite fast now

This gave me reasons to believe that this setup of Docker is actually faster than Docker for Mac. Feel free to try this for yourself using the following instructions.

1. Install requirements

First go to your project directory.

$ cd ~/git/<project>

Install the required software using Homebrew.

$ brew install docker docker-compose docker-machine docker-machine-nfs

$ brew cask install virtualbox

2. Create a virtual machine

$ docker-machine create <your-project-name>

3. Setup NFS

$ docker-machine-nfs <your-project-name> --shared-folder=`pwd`

4. Connect docker to the virtual machine

$ eval $(docker-machine env <your-project-name>)

5. Add virtual machine ip to hosts file

$ echo "$(docker-machine ip <your-project-name>) your-domain.test" | sudo tee -a /etc/hosts

6. Verify that everything works

$ docker ps

You can now start your project containers, e.g.

$ docker-compose up -d

Suppose that your project exposes a website on port 80 or 443, test that you can access it.

$ open http://your-domain.test/

Caveats

A few thing we have to remember:

1 . With every new shell instance we need to repeat step 4.

2 . After a reboot the VM is shutdown, so we need to start it:

$ docker-machine start <your-project-name>