Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker in MacOs is very slow

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?

like image 285
trafficker Avatar asked May 02 '19 11:05

trafficker


People also ask

Why is Docker on macOS slow?

However, if you've ever encountered this on a macOS environment (Docker Desktop), you've probably noticed this to be quite slow at times. The main reason is how file synchronisation is implemented in Docker for Mac. Combined with PHP projects which use a lot of I/O, this can cause quite a performance hit.

How do I speed up Docker on Mac?

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'

Can Docker run on M1 Mac?

Docker image was built in only seven minutes on MacBook M1 Pro, which was even better than the build time on my new VPS. This is not surprising, I gave Docker quite a lot of resources. But it also shows that if there are not too many I/O disk operations, performance is quite good.

Is Docker for Mac good?

Docker for Mac is a game changer. Docker for Mac is the latest offering for Mac which runs as a native OS X application and uses xhyve to virtualize the Docker Engine environment and Linux kernel-specific features. Why is this so great? Because Docker for Mac allows you to easily run Docker as a native app on your Mac.


3 Answers

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
like image 99
Mert Simsek Avatar answered Oct 24 '22 09:10

Mert Simsek


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.

like image 22
Luke Madhanga Avatar answered Oct 24 '22 08:10

Luke Madhanga


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

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.

like image 8
pyb Avatar answered Oct 24 '22 09:10

pyb