Docker on Mac - how to speed it up?

TLDR: As of 2021 - use Linux instead ;-)

Update for 2022: Docker on new MacBook with M1 Pro

Why is Docker on Mac so slow?

Docker on Mac has had some performance issues since the beginning. These are related to volume performance, the way volumes are mounted, and the underlying osxfs filesystem. For some applications, this won’t be a big problem, but all apps which perform many I/O disk operations will be affected and probably not usable.

Why not buy a “normal” computer with Linux on it?

At the very beginning, I was a Windows user and did not need a lot of time to realize that it is not well suited for my programmer career. I prefer the command line over GUI and also Symfony based projects were quite slow on windows back then. Unfortunately, I had only one computer for work and entertainment, and for this second task, Linux was not good enough for me. I was trying to separate work from after-work activities by using two operating systems, switching from one to another based on what I wanted to do. Trust me, this is not a convenient solution, especially in my case (flexible working hours).

Based on that experience I decided to switch to something which combines some of the good features of Linux and the usability of Windows, and I bought my first MacBook Pro.

I was very happy about this decision. It, of course, took me a while to get used to a different system but I still like it and don’t want to change to anything else.

This was the time before Docker, so I had no performance issues with my work-related tasks. Everything has been working well. And then Docker has become a very popular tool, we started to use it in our company and problems started.

Are there any solutions to make Docker on Mac faster?

As I mentioned at the beginning, use Linux instead. Ok, let’s put the jokes aside (is it really a joke?) and investigate a few options to improve Docker performance on macOS.

Benchmark methodology

I used the same methodology for each setup. I was testing the same Rest API endpoint with Apache Benchmarking tool. One hundred sequential requests, without concurrency. Tests were performed on one of our projects, in the dev environment. The same setup that I’m using every day in my work.

I also measured the time needed to build a docker image from scratch. It is also an important factor because it would be nice to not be forced to take one hour break just to build a new image.

In order to have some point of reference, I asked my colleague to test his setup, a computer with much more RAM, CPU, and SSD disk running on Linux.

Docker on Linux performance

Docker for Mac - play with volume configuration

Docker supports three options for setting consistency on volumes definition:

  • consistent (default) - perfect consistency (host and container have an identical view of the mount at all times)
  • cached - the host’s view is authoritative (permit delays before updates on the host appear in the container)
  • delegated - the container’s view is authoritative (permit delays before updates on the container appear in the host)

Setting different volume options of the application may influence performance, and some sources say that such change may make Docker on Mac faster.

If you are using docker-compose then just add the proper option to the volume definition.

version: '3'
services:
  web:
    build: .
    volumes:
      - ./:/var/www:cached

Docker on Mac performance

As you can see on the above chart, Docker for Mac is slow, very slow. There are some differences between each option, but it is not significant and does not improve too much. You can experiment a little bit with different options on your project to get the best performance, but probably you won’t gain too much.

Docker for Mac - support for Mutagen

Mutagen is an open-source tool designed for fast and reliable file synchronization. There are also other similar tools, like docker-sync but Mutagen has better performance and stability. What is interesting for all Mac users is that Mutagen can be used with Docker on Mac, as a tool for syncing files between host and docker volume. It improves performance a lot.

Configuration with docker-compose is easy, you need to set up volume first

volumes:
  code:

use this volume for your service

web:
    build: .
    volumes:
      - code:/code

and setup mutagen to sync host files with this volume

x-mutagen:
  sync:
    defaults:
      ignore:
        vcs: true
    code:
      alpha: "."
      beta: "volume://code"
      mode: "two-way-resolved"

You can find the whole example docker-compose.yml file here.

To run it, use Mutagen Compose, which integrates Mutagen with Docker. All you need to do is to install it and use mutagen-compose up instead of regular docker-compose up.

Looks a little bit complicated but it is not, and the profit is worth it.

Docker on Mac with Mutagen performance

This chart presents mutagen performance in comparison to previous results. Improvement is very noticeable. In my opinion, mutagen is the best option to improve performance, if you want to stick with Docker for Mac.

Linux

I was not joking, seriously Linux is also an option for Mac users. I tested two approaches, VirtualBox and VPS. Both perform quite well.

Linux on VirtualBox

I came up with this solution before mutagen was available and I had been using it for a quite long time before I switched to a different (but similar) approach.

It is a little bit tricky to configure because you must:

  1. set up the virtual machine with a whole Linux based environment
  2. take care of networks
  3. use some tool for file synchronization

But you need to do this only once. I will not describe the whole setup here, it is not in the scope of this post, but because file synchronization performance is the main topic, I can briefly mention what tools I used for that purpose.

Files synchronization

PhpStorm

All PHP developers probably know this IDE and probably most of you are using it. But maybe not everyone knows that it also supports file synchronization. This build-in tool is called Deployment and allows to sync files for example by SFTP. It is easy to configure, it supports automatic upload and many other features. Thanks to that you can work on your project on your Mac like always and all changes will be sent to the virtual machine, where you have Docker running. You can test your application on your normal browser by just going to the virtual machine IP address. It is really fast, and it does not influence performance. Unfortunately, there is one big drawback of this solution, when you need to resync the whole project or send many files at once, PhpStorm will send them one by one which can take some time.

Mutagen

I had been using PhpStorm for some time for the files sync and then I decided to try another tool, mentioned here Mutagen. It can also handle such tasks.

Configuration is very easy, you can have different ones for each project. Just place a file called mutagen.yml inside the project directory with content like this:

sync:
  defaults:
    ignore:
      paths:
        - .DS_Store
  project-code:
    alpha: ~/work/project
    beta: [email protected]:~/work/project
    mode: "two-way-resolved"
    configurationBeta:
       permissions:
          defaultFileMode: 0664
          defaultDirectoryMode: 0755
    symlink:
       mode: "posix-raw"
    ignore:
      vcs: true
      paths:
        - "/build/"
        - "/var/"
        - "/node_mudules/"

Where alpha is your host and beta is your VirtualBox machine. To start synchronization just run mutagen project start. From now your files will be automatically synced with a virtual machine, there are almost no delays and it is a very stable solution. It also collects changed files and sends them as an archive to improve performance. So syncing the whole project won’t take so much time as in the case of PhpStorm based sync.

Time for a benchmark

I also tested different volume options on this setup. The result for mutagen on Docker for Mac is added to this chart to make the comparison easier.

Docker on VirtualBox performance

As you can see, the difference between different volume options is not big. Also, it performs only a little bit better than mutagen. I would even say that this difference is within measurement error.

Linux on VPS

Docker is quite a resource-consuming tool, it does not matter if you are using Docker for Mac or VirtualBox, it will require a lot of CPU and RAM. You can always buy a more powerful machine, but it is an expensive solution and also not too eco-friendly if your current Mac is still working fine. A cheaper option would be to outsource Docker-related stuff to a Virtual Private Server. All heavy tasks can be performed by another machine, Linux based, without performance issues. Such an approach has one more advantage, it is easily scalable, if you need more RAM, CPU or disk, you can add it.

Configuration and setting everything up do not differ much from VirtualBox setup. The main difference is that VPS can be accessed from a public network, so it would be nice to properly secure it with a firewall, VPN, etc. So it is not trivial but I think that everyone with basic DevOps knowledge will be able to configure such an environment.

Docker on VPS performance

I’m using an inexpensive VPS, which is not too powerful. It has a little bit fewer resources than I gave to VirtualBox, this is why it is also a little bit slower.

The main advantage of such a solution is overall Mac performance while using Docker. Docker is running on a different machine so we have whole Mac RAM and CPU for other tasks. Everything else works better, each operation you make is smoother and also the computer is not overheating, and runs quieter.

Currently, I’m using this setup, I switched from VirtualBox to VPS a few months ago and I’m very happy with that.

Build time comparison

Docker build time

This is another aspect I did not mention yet. From time to time there is a need to build Docker containers from scratch. It is generally time-consuming, but can be done in a relatively short time and can also take an hour or more. As you can see Docker for Mac is slow not only in the case of processing requests but it is even worse in the case of building the docker image. Setting up mutagen will shorten this time to a little bit over half of an hour. A similar time is needed for building a container on VirtualBox. What is interesting is that when I did the same test on VPS it was much faster than in other cases. It may also be related to the network connection. I assume that VPS has a lightning-fast internet connection so downloading all required data from the internet took less time than in the case of other setups that were using an internet connection in my home.

One thing worth mentioning here is also the consumption of RAM and CPU during this process. It does not matter if I build a container on VirtualBox or directly on Mac. It is always taking a lot of resources, and it is almost impossible to use a computer during this time. Everything is so slow that the best thing to do during Docker build is to take a break and go for a walk.

Docker Deep Dive - free eBook

Docker like a pro!

GET FREE EBOOK

Summary

Docker for Mac

Cons:

  • very slow
  • resource consuming
  • require a lot of disk space
  • not fully working on new Mac with M1 chip (as of early 2021)

Pros:

  • easy setup - works out of the box

Docker for Mac with Mutagen

Cons:

  • resource consuming
  • require a lot of disk space
  • not fully working on the new Mac with M1 chip (as of early 2021)

Pros:

  • do not require too many changes in configuration
  • it is fast

Docker on VirtualBox

Cons:

  • resource consuming
  • require a lot of disk space
  • more complicated configuration

Pros:

  • it is fast
  • VirtualBox image can be placed on an external drive to save MacBook disk space

Docker for VPS

Cons:

  • more complicated configuration
  • it is not a free solution, you must pay for VPS
  • internet access is required

Pros:

  • it is fast
  • it does not use your Mac resources
  • VPS can be always used for different purposes
  • you can access it from many devices

For me, the choice is simple, buy VPS and have a fast, reliable environment for running Dockerized projects. It is not the cheapest one, but probably it will be cheaper than investing a lot of money in a MacBook with enough disk space, RAM, and CPU to handle Docker, IDE, browser, and other tools at the same time.

Improving your Docker setup may save you a lot of time in the long run. If you are using Docker on a daily basis make sure to check this free ebook on Docker advanced optimization techniques - Docker Deep Dive. You will thank me later ;)

See also my update on that topic for 2022: Docker on new MacBook with M1 Pro.

We also have an article for you if you are not sure if you need to use Docker at all.

icon

Ready to make your SaaS Scalable?

Fix most important issues within days from the kick-off

CONTACT US

Related posts

  • Project Release Checklist

    Project Release Checklist

    At Accesto we recently came up with a project release checklist that helps us to make sure that we did not miss anything. It helped us a lot…

    Read more
  • Page Object Pattern

    Page Object Pattern

    As important and critical unit tests are to our applications, functional testing of the front part should be a must part of any project too…

    Read more