Random Thoughts by Fabien Penso

Improve Docker performance on macOS by 20x

TL;DR: — Docker is great to manage your code, but it’s painfully slow on macOS 1. By using Virtualbox or Parallels, you can make your Rails on Docker on macOS going way faster (x20 on M1…). I ran benchmarks so you don’t have to.

For years, when working on a Rails app, I would embed a Vagrantfile in the repository so anyone joining the project could do a vagrant up and start coding.

Docker became more famous since, and the convenience of Docker Compose to start dependencies like a database, a Redis, or a mail server made it become a solid contender for Vagrant. I use both on macOS because of how slow Docker is.

Docker is great on Linux but painfully slow on macOS and even more on M1. How slow? And how to make it way faster? Run it on a Linux VM. I benchmarked the same code and got the following results.

Benchmark results

/img/docker-benchmark.png

Environment

The two different computers I used to compare test execution time:

  1. A MacBook Pro 16” 2019, 32G RAM, 8-core Intel i9 2.3Ghz
  2. A Mac mini M1 2020, 16G of RAM

The three different environments I used on each computer:

  1. Docker Desktop
  2. Parallels (M1) and VirtualBox (Intel)
  3. Native (with homebrew)

I bootstrapped an empty Rails repository, then ran a rails g scaffold post to have a default test suites, minimal but running. Bundle is then used to run tests:

$ git clone git@github.com:penso/vagrant-vs-docker-rails.git
$ docker-compose up
$ time docker-compose run app bundle exec rake test

Conclusion

Don’t use Docker Desktop on macOS. If you must, use it within a Linux VM. While Docker is very convenient to automate things, it has an x20 incidence on running tests on such a simple code. I noticed the difference might increase with more significant projects.

I’m currently working at Beam, and I moved Docker from our macOS CI servers to Linux servers instead because it would generate too many issues. It also required too much memory.

Read hackernews comments.

  1. I believe because of the way Docker does virtualization on MacOS.