How to Make WordPress Run Faster in Docker on Windows 11: WSL2 Performance Tweaks, Database Optimization, and Best Practices

Learn how to make WordPress run faster in Docker on Windows 11 with WSL2. Optimize file system performance, reduce database slowdowns, and speed up PHP and Apache.

11 views
d

By. Jacob

Jacob Kristensen (Turbulentarius) is a Web Developer based in Denmark. He is currently pursuing a Bachelor's degree in Web Development at Zealand, focusing on learning React and refining his existing skills.

Edited: 2025-02-03 15:13

I recently used my Docker Sandboxer environment to create a localhost development environment for Wordpress, but I was astonished at how slow Wordpress was running in the container.

The reason for the slowness is that Windows and WSL2 communicate through a virtualized file system layer, which significantly slows down file operations when containers access Windows-mounted files. WSL1 does not have this issue because it operates differently, but it lacks full Linux kernel compatibility.

The problem can be easily mitigated by moving the entire project into the WSL2 filesystem (\home\youruser\Work) and running the containers from there, avoiding the Windows file system overhead. E.g. The full path will look like this in Windows' File Manager:

\\wsl.localhost\Ubuntu\home\youruser\Work

The location is also accessible directly from Windows' File Manager (Explorer), where you can simply drag-and-drop your entire work folder to WSL2. Since I work with Web Development, I actually prefer to have my work residing inside WSL filesystem.

By moving your project into the WSL2 filesystem, you eliminate the performance penalty of Windows' file system interaction with Docker. This significantly speeds up file I/O operations, reducing sluggishness in WordPress and other web applications running in containers.

In my case, this led to an impressive 85% reduction in response time. Initially, WordPress took nearly 10 seconds to respond, but now it’s blazing fast at just 1-2 seconds!

Another important optimization is to ensure that your database runs inside WSL2 as well. If your MariaDB or MySQL database resides in a Docker container running on the Windows file system, it will suffer from major slowdowns due to excessive file access latency. Running it directly inside WSL2 eliminates this issue.

To further optimize performance, consider disabling unnecessary background tasks in WordPress. The built-in wp-cron.php system can be inefficient in containerized environments. Instead of relying on wp-cron.php to run on every page load, you can disable it and set up a real cron job:

define('DISABLE_WP_CRON', true);

Then, add a scheduled task in your WSL2 environment to trigger the cron job at regular intervals:

crontab -e
*/5 * * * * curl -s https://your-local-wp-site.com/wp-cron.php > /dev/null 2>&1

Another key factor is the volume mounting strategy. By default, Docker Desktop uses bind mounts to connect Windows directories to containers, which results in high overhead. Instead, use named Docker volumes whenever possible:

docker volume create wp-data

Then, modify your docker-compose.yml file to use this named volume for WordPress:

services:
  wordpress:
    image: wordpress:latest
    volumes:
      - wp-data:/var/www/html
volumes:
  wp-data:

This approach prevents excessive I/O bottlenecks while ensuring faster performance for WordPress.

Another common pitfall is PHP's OPcache settings. If you are developing locally, you should adjust OPcache to work optimally by modifying your php.ini file:

opcache.enable=1
opcache.validate_timestamps=1
opcache.revalidate_freq=0

Setting opcache.revalidate_freq=0 ensures that PHP doesn't cache old versions of your files, which is useful for real-time development.

Additionally, if you use Apache as your web server, enabling mod_expires and mod_deflate can help improve loading times:

a2enmod expires
a2enmod deflate
service apache2 restart

Finally, keeping your environment clean and lightweight is crucial. If you are not using Xdebug during development, disable it to prevent unnecessary overhead:

docker-compose exec wordpress php -d xdebug.mode=off

By applying these optimizations, you can achieve a much faster and smoother experience when running WordPress in Docker under WSL2.

Tell us what you think:

  1. Here is the reason why the Shopware build script may not progress beyond: Dumped plugin configuration.
  2. How to fix a problem with composer in Docker that results in: failed to open stream: No such file or directory error for InstalledVersions.php.
  3. How to fix a problem that happens when another container is occupying the specified port in Docker.

More in: Docker