42

I have this docker-compose.yml:

version: "3.1"
services:

    memcached:
      image: memcached:alpine
      container_name: universal-memcached2

    redis:
      image: redis:alpine
      container_name: universal-redis2

    mariadb:
      image: mariadb:10.4
      container_name: universal-mariadb2
      working_dir: /application
      volumes:
        - .:/application
        - "../data/db:/var/lib/mysql" # skasowac
      environment:
        - MYSQL_ROOT_PASSWORD=Haslo
        - MYSQL_DATABASE=sample
        - MYSQL_USER=user
        - MYSQL_PASSWORD=Haslo
      ports:
        - "8083:3306"


    webserver:
      image: nginx:alpine
      container_name: universal-webserver2
      working_dir: /application
      volumes:
          - .:/application
          - ./phpdocker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf
      ports:
       - "8080:80"

    php-fpm:
      build: phpdocker/php-fpm
      container_name: universal-php-fpm2
      working_dir: /application
      volumes:
        - .:/application
        - ./phpdocker/php-fpm/php-ini-overrides.ini:/etc/php/7.3/fpm/conf.d/99-overrides.ini

    volumes:
        generated:
        var:
        static:

    phpmyadmin:
      image: phpmyadmin/phpmyadmin
      links:
        - mariadb
      environment:
        PMA_HOST: mariadb
        PMA_PORT: 3306
      ports:
        - '8081:80'

When I run my newest project in symfony 4 on it, it works very slowly... :(

I have new MacOs and Docker Desktop. I'm currently learning the Symfony and Laravel framework, but this is very slow for Docker. It is not even working on it.

How can I repair it?

trafficker
  • 601
  • 1
  • 11
  • 20
  • 3
    There is [a section about this in the Docker Desktop for Mac documentation](https://docs.docker.com/docker-for-mac/osxfs/#performance-issues-solutions-and-roadmap). IME these problems are less significant if you `COPY` your built and tested application into an image, but it's a much greater issue with a `volumes:` bind-mounting your entire application source like you have here. – David Maze May 02 '19 at 11:28
  • Can you try and increase the memory allocated to Docker? It is in Preferences -> Advanced. Right click on the whale icon in the toolbar to open preferences. – Mihai May 02 '19 at 11:35
  • I have in Preference CPU: 4, Memory: 8GB, Swap:2.5GB. My Mac has: i7/16GB Ram/512SSD – trafficker May 02 '19 at 11:40
  • yes, I need Docker to learn, not to upload a ready and tested application – trafficker May 02 '19 at 11:42
  • 0 Could you share more details about which part that is really slow? I mean you can check if it's the code itself has an issue or maybe the database or maybe the docker container itself tries to allocate more memory. another trick is to login to your container and checks the memory usage, cpu usage,...etc – Alaa Attya Mohamed May 02 '19 at 14:01
  • Take a look at my answer on this post. https://stackoverflow.com/questions/31324922/docker-inter-container-communication-painfully-slow-mac-os-x/59573818#59573818 that might help. – Devendra Mistri Jan 03 '20 at 05:59

9 Answers9

28

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
JJJ
  • 32,902
  • 20
  • 89
  • 102
Mert Simsek
  • 1,550
  • 9
  • 19
  • 4
    "Unfortunately, Mac OS and Windows cannot provide this." - not true, windows can provide the linux kernel through WSL2. Docker works very fast there comparing with the windows app. – Grzegorz Pietrzak Sep 10 '21 at 20:24
  • 2
    @GrzegorzPietrzak WSL2 is still itself a VM and requires Hyper-V from my understanding. – Hayden Jun 01 '22 at 17:12
17

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.

Luke Madhanga
  • 6,871
  • 2
  • 43
  • 47
  • 1
    Didn't seem to work for me. See [this question](https://stackoverflow.com/questions/63742046/mounted-volumed-performance-in-osx) – benzaita Sep 04 '20 at 13:54
16

Using VirtioFS significantly improves Docker performance on macOS.

macOS 12.5 and Docker Desktop 4.15

Docker Desktop Preferences > General

[x] Use Virtualization framework (select this option)
Choose file sharing implementation for your containers: pick VirtioFS

macOS 12.3 (Intel) or 12.2 (Apple M1) and Docker Desktop 4.6, you can enable the experimental feature :

To enable virtiofs in Docker Desktop:

Ensure that you are using Docker Desktop version 4.6, available here

  • Navigate to ‘Preferences’ (the gear icon) > ‘Experimental Features’
  • Select the ‘Use the new Virtualization framework’ and ‘Enable VirtioFS accelerated directory sharing’ toggles
  • Click ‘Apply & Restart’

Speed boost achievement unlocked on Docker Desktop 4.6 for Mac - March 16 2022

On a MacBook Pro 2019, 2.6 GHz 6-Core Intel Core i7, even simple commands such as docker ps execute in less than a second. It used to take 3 seconds before.

pyb
  • 4,813
  • 2
  • 27
  • 45
7

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.

Mohammad Mehdi Habibi
  • 1,601
  • 1
  • 14
  • 30
5

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.

Kwadz
  • 2,206
  • 2
  • 24
  • 45
  • Apparently the downvoter didn't understand that Mutagen is used with Docker to sync volumes (not as a Docker replacement). So, I updated the answer accordingly. – Kwadz Jan 19 '21 at 03:12
4

I had the same issue, where requests could take upwards of 20 seconds!

The fix was to disable "Use the new Virtualization framework"

enter image description here

I also tried the "VirtioFS" experimental option which made it faster but it would constantly lock up. Disabling them both fixed all issues.

1

To anyone still having this issue, I have read a post on gitHub by StefanPrintezis here. Basically, when you're not debugging, turn off xDebug. It worked like a charm for me.

Configuration : Macbook Pro 2018, PHPStorm 2021.3.1, Docker Desktop 4.5.0 (74594) with 2 CPU / 4GB RAM. Using default volume config.

Dharman
  • 30,962
  • 25
  • 85
  • 135
El Momo
  • 11
  • 1
-1

This is know. Issue with docker and the Mac... they are not best buddies, but they can play with each other.

Add :cached to the volumes and the slowness will be manageable.

Alexander Dimitrov
  • 944
  • 1
  • 6
  • 17
-7

I recommend using Parallels (the best virtualisation for macOS). Then:

  1. Install Linux
  2. Install ssh server
  3. Install docker

If you use VSCode, you can install SSH extension and it is fast enough for editing files. You can also access your website on macOS via virtual machine's IP address.

Peter
  • 21
  • 3