Docker in MacOs is very slow

Solution 1:

As a matter of fact, Docker needs a plain Linux kernel to run. Unfortunately, Mac OS and Windows cannot provide this. Therefore, there is a client on Mac OS to run Docker. In addition to this, there is an abstraction layer between Mac OS kernel and applications (Docker containers) and the filesystems are not the same. Because of that, Docker runs on Mac OS slowly. You are not able to run the Docker on Mac OS like on Linux.

If I need to give some instances about real use-cases. I have the same machine. So, I use Symfony 4 on Docker v18 on Mac OS Mojave. This is my Symfony total execution time on Docker. (Obviously, it depends on your frontend and database queries but I try to enlighten you for main logic.)

  • first time rendering 12000 ms
  • with Symfony cache: 344 ms
  • with Docker cache(:cached property of Docker for volumes): 195 ms

As long as I use Symfony without Docker, the following is my total execution time.

  • without Docker, with Symfony cache: 82 ms

Whereas, we could do some improvements to get better workspace. For example, you can use volumes like this,

volumes:
        - .:/application:cached

Solution 2:

As mentioned in other answers, the issue comes down to the way Docker interacts with the file system on OSX.

I found a great article on how to make things a lot faster on OSX:

https://vivait.co.uk/labs/docker-for-mac-performance-using-nfs

From the article, the real solution is to change the file system to use NFS, but as I know nothing about Docker (it was set up for me at work), I opted to use an option available in newer versions of Docker

I added :delegated to all of the options under volumes in docker-compose.yml. After restarting my containers, every was nearly twice as fast as before. Not as fast as native, but fast enough.

Example from article:

app:
    build:
        context: .
        dockerfile: ./docker/app/Dockerfile
    working_dir: /app
    user: "www-data"
    volumes:
        - ./:/app:delegated
        - ./:/another_folder:delegated

The NFS example is quite long and requires basically copying half of the article so I won't add it in, but this :delegated solution is good place to start.

Solution 3:

One option would be using http://docker-sync.io/ But you will not get the same performance as you would in a Linux-based machine.

Solution 4:

Use Mutagen to sync volumes

It's a known issue that Docker for Mac is slow when using shared volumes containing a big amount of files, see https://github.com/docker/for-mac/issues/77.

So, some workarounds came like :cached and docker-sync. However it's not totally satisfactory because it's still slow compared to native (Linux) and docker-sync consumes a lot of resources when syncing.

That being said, there is a good alternative, not to say perfect, which is Mutagen. It's almost as fast as native shared volumes with Linux!

A benchmark is available:

enter image description here Source: article on Medium

I created a full example for a Symfony project to show how to use it, but it can be used for any type of project in any language.