Caching Docker volumes on macOS

Caching your Docker volumes to improve performance

TL;DR Adding :cached to your Docker bind mounts can improve performance in Docker for Mac at the tradeoff of some latency for your file changes to appear in the container.


Here at Brand New Box we run all of our applications inside of containers on our development environments. This provides us several efficiencies outlined here.

However there is one downside of this approach, our applications run a little slower on our Mac development machines since Docker for Mac uses a virtualization layer to function.

One of the slowdowns we have is file system access. We bind mount all of our project files into the container while developing and our applications need to read from the file system quite a bit, when they are rendering a view or reloading Ruby code. Whenever we need that file system access the container goes through Docker's virtualization layer and so we get some hefty performance penalties. This was a very noticeable slowdown and issues were filed about it.


However the Docker team has starting adding things into Docker for Mac to help with the performance of bind mounts. They wrote up a document on User-Guided caching and outlined some performance improvements they were making by adding different levels of file system consistency between the host and container.

The take-away from that guide was that for our type of work, if we could live with a little latency in file changes that we made in our editors appearing in the container then we could use a cached consistency level in our bind mounts and see much improved performance.

To change this we simply had to add :cached to all of our bind mounts to see this improved performance.

Our volume definitions in our docker-compose.yml files changed from

volumes:
  - .:/app
to
volumes:
  - .:/app:cached

Using the benchmarking tool wrk we were able to go from 0.33 requests/second to 0.9 requests/second! And when measuring whole page loads where all of our assets and images get pulled in we saw an improvement of 3.5x on some of our applications!

In practice, we've never noticed any of the latency issues with files taking a bit longer to show up inside of the container and it's greatly sped up our development workflow!

We'd reccommend trying out the cached option and benchmarking your applications to see if it helps you!