Yanick van Barneveld
Yanick van Barneveld

Reputation: 312

Docker for WordPress slow

Problem:

I have a problem with WordPress & Docker because the slow loading time (+- 7 seconds) of my website. I am not sure why this happends but I think it has something to do with the external database or the shared volumes.

Setup:

I have a custom Dockerfile built on WordPress with XDebug and Mailhog. This Dockerfile is included within my docker-compose.yml, the other services my docker-compose contains are WP-CLI and Mailhog. My Database is hosted on an Amazon RDS so I can share it with my colleagues.

Code:

My Dockerfile looks like this:

FROM wordpress:latest

# Plugins & Media
RUN mkdir -p /var/www/html/wp-content/plugins
RUN mkdir -p /var/www/html/wp-content/uploads

RUN chown -R www-data:www-data /var/www

RUN find /var/www/ -type d -exec chmod 0755 {} \;
RUN find /var/www/ -type f -exec chmod 644 {} \;

# Mailhog
RUN curl --location --output /usr/local/bin/mhsendmail https://github.com/mailhog/mhsendmail/releases/download/v0.2.0/mhsendmail_linux_amd64 && \
    chmod +x /usr/local/bin/mhsendmail

RUN echo 'sendmail_path="/usr/local/bin/mhsendmail --smtp-addr=mailhog:1025 [email protected]"' > /usr/local/etc/php/conf.d/mailhog.ini

# Xdebug
ENV XDEBUG_PORT 9000

RUN yes | pecl install xdebug \
    && echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.profiler_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.profiler_output_name=cachegrind.out.%t" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "xdebug.profiler_output_dir=/tmp" >> /usr/local/etc/php/conf.d/xdebug.ini \
    && echo "max_input_vars=2000" >> /usr/local/etc/php/conf.d/custom.ini \
    && rm -rf /usr/local/etc/php/conf.d/opcache-recommended.ini

EXPOSE 9000

My Docker-compose.yml looks like this:

version: "3.7"
services:
    wordpress:
        container_name: "${PROJECT_NAME}_wordpress"
        restart: always
        build:
            context: ./
            dockerfile: ./Dockerfile
        ports:
            - "8888:80"
            - "443:443"
        environment:
            WORDPRESS_DB_NAME: "${PROJECT_NAME}"
            WORDPRESS_DB_HOST: "${MYSQL_HOST}"
            WORDPRESS_DB_USER: "${MYSQL_USER}"
            WORDPRESS_DB_PASSWORD: "${MYSQL_PASSWORD}"
            WORDPRESS_DEBUG: 1
            XDEBUG_CONFIG: remote_host=host.docker.internal
            WORDPRESS_CONFIG_EXTRA: |
                define('FS_METHOD', 'direct');
        volumes:
            - "wordpress:/var/www/html"
            - "./build/uploads:/var/www/html/wp-content/uploads:cached"
            - "./build/plugins:/var/www/html/wp-content/plugins:cached"
            - "./build/themes:/var/www/html/wp-content/themes:cached"
    cli:
        container_name: "${PROJECT_NAME}_cli"
        image: "wordpress:cli"
        volumes:
            - "wordpress:/var/www/html"
            - "./build/plugins:/var/www/html/wp-content/plugins:cached"
        depends_on:
            - wordpress
    mailhog:
        container_name: "${PROJECT_NAME}_mailhog"
        image: mailhog/mailhog
        depends_on:
            - wordpress
        ports:
            - "1025:1025"
            - "8025:8025"
volumes:
    wordpress: null

But I can't find out why it is so slow; I got version 2.1.09.3 of Docker Desktop and working on a fast Mac or Windows.

Can someone help me or point me in the right direction?

Edits

  1. If I look in the docker stats my CPU is around 0.01% and my MEM is around 2.73% so that can't be the problem.
  2. Found out the biggest problem is connecting to the external database. If I move over to a local database the loading time is a lot faster (+- 1 sec).

Upvotes: 5

Views: 5179

Answers (1)

molavec
molavec

Reputation: 9117

There some issues for Mac & Windows Volume performance.

Link for more details:

Docker Wordpress super slow


In Mac and Windows there are some volumes performance issues that we should consider.

I made change in my docker-compose.yml

Note as I changed the short syntax to long syntax.

This notation permits add consistency option.

I added wp-content and php-conf (to get php.ini) because they are files directory most frequently called every time when a Wordpress page is loaded in browser.

services:
    wordpress:

        ...

        volumes:
            - ./data:/data
            - ./scripts:/docker-entrypoint-initwp.d
            #- ./wp-content:/app/wp-content
            - type: bind
              source: ./wp-content
              target: /app/wp-content
              consistency: cached
            #- ./php-conf:/usr/local/etc/php
            - type: bind
              source: ./php-conf
              target: /usr/local/etc/php
              consistency: cached

Upvotes: 1

Related Questions