Introduction

This post will cover how I host local copies of PHP websites running WordPress and Laravel with Docker Compose. This removes the need to install PHP, Composer, Nginx/Apache, MySQL, NPM/Node, etc locally on your host machine.

I run my local development environments within Docker to avoid bloating my system. This means all of the software required to run websites operates within their unique containers. I can spin them up or down as I require, and rebuild them easily to get updates, all without polluting my computer and removing the risk of breaking things during an update.

Pros and Cons of this Approach

I’ve done a lot of PHP development both from my system directly and from container-based setups. Based on my experience and personal opinions, here’s my pro/con list for working within Docker containers vs working on the host OS.

Pros of Using Docker for PHP Development:

  • The PHP version can be easily controlled by simply swapping to another container with a different version running.
  • It’s easy to monitor the performance and sizes of code and databases.
  • My development environment is identical on multiple computers which I swap between regularly.
  • I have identical development workflows on both Windows 11 and Linux.
  • Tweaking and tuning PHP and NGINX settings is simple, as all the configuration files are in one place on the host machine.

Cons of Using Docker for PHP Development:

  • Building the containers can take a while (10+ minutes). *But I only rebuild them approximately once a month.
  • When using Windows, I can’t mount a container’s files directly into the Windows filesystem via a volume without large performance penalties. *Using blind docker volumes directly resolves this, but means I can’t navigate files with Windows Explorer.

Over the course of my career, I’ve lost multiple days of work due to development environment issues. I’ve had blockers ranging from the full LAMP stack seemingly not working, to being unable to get a single PHP extension like xDebug to work correctly. While it took a lot of work to get my Docker Compose environment to where it is now, I don’t have to ever restart that work from the beginning. No matter what computer I use, be that Windows or Linux, I know that I can get set up and ready for unrestricted PHP development within 30 minutes.

Note: I don’t own or use any Apple devices personally or professionally, so I can’t comment on this Docker Compose setups compatibility with their Silicon chips.

How to Host WordPress and Laravel with Docker Compose

The social share image for the jmwhitworth/docker-php-lamp repository.

To get started, go to this Github Repo (written and maintained by me) and clone it to your computer.

Following the instructions from the README.md file, you’ll need to do the following:

  • Build the Docker Compose file to get all of the containers ready.
  • Create an Nginx config file from the provided template for the website you’re working on locally.
  • Restart the Nginx container once the config file is ready.
  • Connect to the appropriate PHP container via VSCode’s Docker extension to get coding.

For reference, here’s an example of the NGINX configuration file I use to host a local copy of my website:

server {
    listen 80;
    server_name jackwhitworth.com.localhost;

    root /var/www/html/jackwhitworth.com/public;
    index index.php index.html;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Uncomment if site is WordPress
    include /etc/nginx/additional/wordpress.conf;

    # Choose PHP version
    #include /etc/nginx/php/7.4.conf;
    include /etc/nginx/php/8.2.conf;
    #include /etc/nginx/php/8.3.conf;
}

Note: I’d recommend reading through the readme once or twice as to get started, as the above list is a very high-level explanation, whereas the readme is much more comprehensive. If you find anything unclear, let me know and I’ll be sure to update the readme so it’s clearer for anyone else in the future!

Hosting WordPress and Laravel via Docker in Production

The above repo that I’ve linked is not intended for production environments. Production environments are required to be secure and optimised. This suite of containers I’ve been discussing are set up and ready for development only. While you could use this to host public-facing websites, I cannot accept any responsibility should you choose to do so.

Other features of this Docker Compose PHP development environment

In no particular order, for those who are considering using this Docker-based PHP development environment, here is a list of some other features you get with my solution:

  • PHP:
    • 8.3 (Imagick not currently supported)
    • 8.2
    • 7.4
    • Extensions:
      • exif
      • gd
      • gmp
      • mysqli
      • imagick
      • intl
      • opcache
      • pdo_mysql
      • redis
      • zip
      • xdebug
      • composer
      • calendar
  • NGINX
  • Mysql
  • Redis
  • NodeJS & Yarn via NVM
  • Heroku CLI
  • ZSH via Oh-My-Zsh
  • AWS CLI

Leave a Reply

Your email address will not be published. Required fields are marked *