Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Wordpress super slow

I have Wordpress running inside Docker, for local development, and it's super slow. My docker-compose.yml looks like this:

version: '3.3'

services:
  db:
    image: mysql:5.7
    volumes:
      - ./db_data:/var/lib/mysql
      - ./dbconfig.cnf:/etc/mysql/conf.d/custom.cnf
    restart: always
    ports:
      - "3308:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: wp_database
      MYSQL_USER: db_user
      MYSQL_PASSWORD: some_secure_password

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    ports:
      - "80:80"
      - "443:443"
    restart: always
    volumes:
      - ./wp-content:/var/www/html/wp-content
      - ./.htaccess:/var/www/html/.htaccess
      - ./wp-config.php:/var/www/html/wp-config.php
      - ./logs/debug.log:/var/www/html/wp-content/debug.log
volumes:
  db_data: {}
  wp_content: {}

As far as I read online, it might be the reason I am mounting the wp-content volume, which causes super slow page loading (takes like half a second to load each file, e.g. a jquery file, and it has to load a ton of files for one page).

Is there a solution for this? I read about NFS, but it didn't work for me to configure NFS with docker-compose, somehow I keep getting "permission errors". On the other hand, the Docker interface of macOS already shows me a "Shared folder" tab but I don't know whether I am using those shared folders at the moment or just mounting them again.

Any help is appreciated.

like image 603
Erik van de Ven Avatar asked Jan 21 '19 14:01

Erik van de Ven


1 Answers

TL;DR Mount to temporary folder on container, sync that folder with Bindfs to public server folder. Serving WP site with direct mount is slow because container has to access Host files one by one, which is a heavy process. Serving from public folder while files being part directly of container is much faster.

I've encountered exactly the same problem with local WordPress on Docker Compose development. It doesn't matter how fast your computer might be, it'll still be slow when mounting the folders in the containers.

I also tried solutions like NFS and other recommendations like properly excluding the project in the antivirus, adding .dockerignore, etc. which at best improve just slightly the performance.

While browsing for a similar speed improvement I came across this Dockerfile at the WordPress Starter repository https://github.com/visiblevc/wordpress-starter/blob/master/Dockerfile. If you look at this file you'll see that the way they intialize and mount the project in the container is by mounting it not to, let's say, /var/www/html/ directly, but a temporary folder instead. Then they sync this temporary folder to /var/www/html/ through bindfs. This way every time you load a WordPress page in the browser it'll be lightning fast because it won't have to access and read the Host files on every request. The WordPress files are part of the Linux container. When you make changes to your code those changes will reflect on the container temporary folder and bindfs will instantly sync those changes over to the public container folder, and same the other way around. All changes made on the public folder will be synced over to the temp folder, and from there to your Host project files.

like image 94
Arman Shahinyan Avatar answered Oct 12 '22 22:10

Arman Shahinyan