WordPress Local Development Environment with Docker Compose

Posted September 29, 2023
WordPress Local Development Environment with Docker Compose

Build Docker local development environment for WordPress. You’ll create a Docker Compose container using MySQL and PhpMyAdmin for dB management.

Docker creates a self-contained environment. You’ll use a few simple commands to spin up a full-fledged virtual machine that creates a local web server for any WordPress local-related development.

Docker Compose allows you to run multi-container Docker applications. You need this for your WordPress to run a WordPress database and a database viewer container.

To make this WordPress Local Development Environment with Docker Compose fun, you will learn the following topics:

  • Create a MySQL database container for WP Local Development using Docker Compose yaml
  • Spin a Local WordPress Environment within Docker using Docker Compose
  • Using PhpMyAdmin to access your local WordPress MySQL container
  • Add Local Volumes to manage WordPress and MySQL persistent Development Environment
  • Learn how to modify uploads.ini files and add WordPress Local Development custom PHP settings
  • Access local WordPress files. Your local storage will reflect Any changes you make to your Docker WordPress Development Environment.

Are you ready to learn these mind-blowing steps and elevate your WordPress Local Development Environment with Docker Compose to new heights? Dive in and find out.

Prerequisites

Setting Docker Compose to run a WordPress Local Development Environment isn’t out of the box. Therefore, whatever your OS is, install Docker for Desktop and get up and running on your machine. Here is a link to download it.

Related: Host WordPress on AWS EC2 Ubuntu AMI with SSL Domain Name

Running a Local Development WordPress MySQL with Docker

WordPress can’t work without a relational database. MySQL is a good choice unless you want to use other SQL RDMS, such as MariaDB.

So, let’s spin a local container that will allow you to create your WordPress database environment.

Related: Boost WP Speed with Redis Object Cache Plugin for WordPress

On your text editor, create a docker-compose.yml file. This file will contain all the services you need to have your WordPress Local Development ready.

In your docker-compose.yml, Run a Docker MySQL server container as follows:

version: '3.7'
services:
  # MySQL service
  db:
    image: mysql:5.7
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: wp_db
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: user_password
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-u", "root", "-p", "user_password"]
      interval: 1m
      timeout: 10s
      retries: 3
    volumes:
      - db:/var/lib/mysql # Mount a volume to persist MySQL data

Key points to note:

  • A MySQL server requires a password, user, and the database you will use to host your WordPress database table.

Creating Local Development WordPress with Docker

With the database ready, let’s create a WordPress service. In this case, you’ll modify the uploads.ini file and add WordPress Local Development custom PHP settings. This will allow your local WordPress environment to have properties such as being able to upload files to your WordPress server.

So, in your project directory, create a config folder and add an uploads.ini file with the following custom PHP settings:

file_uploads = On
max_execution_time = 600
memory_limit = 1G
upload_max_filesize = 1G
post_max_size = 1G
extension=mysqli

This file Must be uploaded to WordPress. Let’s create a WordPress service to build a WordPress local Docker container. In your docker-compose.yml add the following WordPress Configurations:

  # WordPress service
  wordpress:
    image: wordpress:latest
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: user_password
      WORDPRESS_DB_NAME: wp_db
    ports:
        - "8080:80"
    volumes:
    # Mount a volume to persist WordPress files
      ["./wordpress:/var/www/html"] 
    volumes:
      - ./config/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    depends_on:
      - db

In the above setup:

  • The environment variables point to the database user who will manage WordPress. So, ensure such changes reflect as such.
  • Volume ./config/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini will copy your uploads.ini file, and your WordPress container will be able to access it within Docker.
  • Volume ["./wordpress:/var/www/html"] is unique in this setup. Adding it this way will allow Docker to add all WordPress files to your project directory so you can access WordPress files and changes locally.

Managing Local Development WordPress database with PhpMyAdmin

PhpMyAdmin creates a user interface that you can use to manage your database used to host WordPress tables. In this example, you will use your PhpMyAdmin as follows:

version: '3.7'
services:
  # MySQL service
  db:
    image: mysql:5.7
    restart: unless-stopped
    environment:
      MYSQL_ROOT_PASSWORD: root_password
      MYSQL_DATABASE: wp_db
      MYSQL_USER: wp_user
      MYSQL_PASSWORD: user_password
    healthcheck:
      test: ["CMD", "mysqladmin", "ping", "-u", "root", "-p", "user_password"]
      interval: 1m
      timeout: 10s
      retries: 3
    volumes:
      - db:/var/lib/mysql # Mount a volume to persist MySQL data

  # WordPress service
  wordpress:
    image: wordpress:latest
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wp_user
      WORDPRESS_DB_PASSWORD: user_password
      WORDPRESS_DB_NAME: wp_db
    ports:
        - "8080:80"
    volumes:
    # Mount a volume to persist WordPress files
      ["./wordpress:/var/www/html"] 
    volumes:
      - ./config/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    depends_on:
      - db

  # PHPMyAdmin service
  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    ports:
    # Map port 8081 on the host to port 80 in the container
      - "8081:80" 
    environment:
      PMA_HOST: db
      PMA_USER: root
      PMA_PASSWORD: root_password
    depends_on:
      - db

volumes:
# Define a volume for WordPress files
  wordpress:
  # Define a volume for MySQL data 
  db:

To run your local WordPress docker app, use the following command. Ensure you are running it in the directory hosting your docker-compose.yml file:

docker-compose up -d

While running this command, you will be asked to share the files you added to your configuration setup. Go ahead and do so.

WordPress Local Development Environment with Docker Compose

Once you have your containers created, your setup should be ready:

WordPress Local Development Environment with Docker Compose

Now navigate to Docker and check your running container as follows:

WordPress Local Development Environment with Docker Compose

Let’s confirm if we can now use the WordPress Local Development Environment setup:

WordPress will be exposed on port 8080, So open it on your browser http://localhost:8080/ as follows:

WordPress Local Development Environment with Docker Compose

Click Continue to set it up as follows:

WordPress Local Development Environment with Docker Compose

Finally, log in to WordPress Local Development Environment:

WordPress Local Development Environment with Docker Compose

WordPress Local Development Environment with Docker Compose

And there you finally have WordPress Local Development Environment ready to use:

WordPress Local Development Environment with Docker Compose

The primary test is to check if you can add file uploads, so Navigate to Media and add New:

WordPress Local Development Environment with Docker Compose

Uploads are working, and you even see Maximum upload file size: 1 GB based on your uploads.ini settings.

To finally access your files, check the WordPress folder, and your files will be listed as such:

WordPress Local Development Environment with Docker Compose

If you want to manage the Local Development WordPress database with PhpMyAdmin, open http://localhost:8081/ on your browser, and you will have it ready:

WordPress Local Development Environment with Docker Compose

Conclusion

This guide has taught you how to create a WordPress Local Development Environment with Docker Compose. In summary, you have learned the following topics:

  • Created a MySQL databases container for WP Local Development using Docker Compose yaml
  • Spinned a Local WordPress Environment within Docker using Docker Compose
  • Used PhpMyAdmin to access your local WordPress MySQL container
  • Added Local Volumes to manage WordPress and MySQL persistent Development Environment
  • Learned how to modify uploads.ini files and add WordPress Local Development custom PHP settings
  • Accessed local WordPress files. Your local storage will reflect Any changes you make to your Docker WordPress Development Environment.
WordPress Local Development Environment with Docker Compose

Written By:

Joseph Chege