It’s a common practice to debug PHP using Xdebug. Xdebug is an extension for PHP to assist with debugging and development.

In this article, I’m going to introduce a more convenient way to quickly set up development environment with PHPStorm, Docker, Docker compose and Xdebug. The source code for docker compose configs are available in the Github: https://github.com/liqili/wordpress-xdebug-docker/

First of all, suppose we have installed Docker and Docker compose in a Linux-like OS, and installed a Wordpress site in production. Then, what we need to do is to setup a development environment for that site so that we can customize plugins or what else. This post will help you learn how to:

  • Use Docker compose to mange docker containers.
  • Export production db to dev Mysql docker container.
  • Integrate Xdebug plugin with docker container as well as PHPStorm.
  • How to initialize MySQL container with sql script file.

docker-compose.yml for container management

YAML

version: "3"

services:

  mysql:

    image: mysql:5.7

    container_name: mysqldb

    ports:

      - "3306:3306"

    command: --max_allowed_packet=1073741824 --sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

    environment:

      MYSQL_ROOT_PASSWORD: 123

      MYSQL_DATABASE: log

    volumes:

      - ./configs/db/:/docker-entrypoint-initdb.d ## mysql will execute the initial sql script under the mount point one by one

      - db-data:/var/lib/mysql

  wordpress-app:

    build:

      ./configs/php/

    container_name: wordpress-app

    ports:
 - "80:80"

      - "443:443"

    restart: always

    environment:

      XDEBUG_CONFIG: "remote_host=host.docker.internal" #This config works for MacOS, otherwise should be ip address of the host machine because docker container cannot find the host by localhost.

    volumes:

      - ../local-wordpress:/var/www/html ## mount your local wordpress site here

      - /tmp/wp-errors.log:/tmp/wp-errors.log

    depends_on:

      - mysql

volumes:

  db-data:

#php #docker #mysql #wordpress #docker compose #phpstorm #xdebug

Setup Wordpress Debug Environment With Docker and Xdebug
13.40 GEEK