文件夹映射时,XDebug 无法与 WordPress 容器一起使用

XDebug not working with WordPress container when folder mapped

我在 docker-composer 上有一个 WordPress 运行,我希望能够调试它。

我使用这个存储库作为基础Wordpress Docker xDebug Boilerplate并且我改变了一些东西。

这是我的docker-撰写:

version: '3.9'

services:

  db:
    platform: linux/x86_64
    image: mysql:8.0
    container_name: db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - 3306:3306
    environment:
      MYSQL_USER: db_user
      MYSQL_PASSWORD: db_password
      MYSQL_DATABASE: db_name
      MYSQL_ROOT_PASSWORD: root
    volumes:
      - db_data:/var/lib/mysql

  wordpress:
    depends_on:
      - db
    build: .
    image: wordpress:latest
    restart: always
    ports:
      - 8888:80
    env_file: .env
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_NAME: db_name
      WORDPRESS_DB_USER: db_user
      WORDPRESS_DB_PASSWORD: db_password
      PHP_EXTENSION_XDEBUG: wp_password
    volumes:
      - wp_data:/var/www/html
      - ./wp-content/:/var/www/html/wp-content/

volumes:
  db_data:
  wp_data:


我的 Dockerfile:

FROM wordpress:latest

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && echo "xdebug.mode=debug" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.client_host = host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

我在项目根目录下创建的名为 wp-content 的文件夹中有一些内容:

我的问题是:映射文件夹 wp-content 时,调试器不工作。我检查了端口配置等

我正在使用 VSCode,这是我的 launch.json 文件:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "pathMappings": {
        "/var/www/html": "${workspaceFolder}/cms"
      },
      "xdebugSettings": {
        "max_data": 65535,
        "show_hidden": 1,
        "max_children": 100,
        "max_depth": 5
      }
    }
  ]
}

但是,如果 /var/www/html 的内容完全映射到另一个文件夹(例如 cms),则效果很好。

# wordpress service
    volumes:
      - './cms:/var/www/html'

如何让调试器将 wp-content 复制到 WordPress 容器内?

在我的神奇工作之前,我必须添加大量配置: 安装 PECL 后

# install xdebug
RUN echo "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN echo "xdebug.idekey=\"PHPSTORM\"" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_port=9000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_autostart=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo "xdebug.remote_host=docker.for.win.localhost" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
RUN echo 'xdebug.mode=debug' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.client_host=host.docker.internal' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.remote_autostart=0' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.remote_enable=1' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.default_enable=0' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.remote_host=host.docker.internal' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.remote_port=' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.remote_connect_back=0' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.profiler_enable=0' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.remote_log="/tmp/xdebug.log"' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.profiler_output_dir="/var/www/html/profiler"' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.profiler_output_name="cachegrind.out.%p"' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.cli_color=1' > /usr/local/etc/php/php.ini
RUN echo 'xdebug.profiler_append=1' > /usr/local/etc/php/php.ini

嗯,问题是我在 launch.json 文件中的配置有误。 我的 pathMapping 设置为收听 cms 文件夹。

我只需要从 $worspaceFolder}/cms 更改为 $worspaceFolder}/wp-content

这是正确的launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Listen for XDebug",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "pathMappings": {
        "/var/www/html/wp-content": "${workspaceFolder}/wp-content"
      },
      "xdebugSettings": {
        "max_data": 65535,
        "show_hidden": 1,
        "max_children": 100,
        "max_depth": 5
      }
    }
  ]
}