使用 Docker Compose 和 WordPress 镜像设置 Xdebug

2021-12-26 00:00:00 docker docker-compose php xdebug

我正在尝试使用 docker-compose 设置 Xdebug.我使用的是 PhpStorm IDE,我使用的 Docker 映像安装了 PHP 7.2.我正在尝试在此 WordPress 图像的上下文中执行此操作.这是我当前的 docker-compose.yml 文件:

 版本:'3'服务:D b:图像:mysql:5.7端口:- 3306:3306"卷:- db_data:/var/lib/mysql重启:总是环境:MYSQL_ROOT_PASSWORD:somewordpressMYSQL_DATABASE:wordpressMYSQL_USER:wordpressMYSQL_PASSWORD:wordpress网络构建脚本:建造: .依赖于取决于:- D b端口:- 8028:80"- 8029:8029"卷:- ./themes/XXXXX:/var/www/html/wp-content/themes/XXXX- ./plugins/XXXX:/var/www/html/wp-content/plugins/XXXXX- ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX- ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX- ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX- ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX- ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX- ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX- ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX重启:总是环境:WORDPRESS_DB_HOST: db:3306WORDPRESS_DB_USER:wordpressWORDPRESS_DB_PASSWORD:wordpressVIRTUAL_HOST:leasepilot.local卷:数据库数据:

然后我标记为 web-build-scripts(当然是构建的)的 Dockerfile 看起来像这样:

来自 wordpress:latest工作目录/var/www/html运行 pecl install xdebug-2.6.0 &&docker-php-ext-enable xdebug &&apt-get 更新 &&apt-get install nano &&出口条款=xterm复制搜索-替换-DB/./wp-content/Search-Replace-DB/复制上传/./wp-content/uploads/复制 ./docker-config/xdebug.ini ../../../usr/local/etc/php/conf.d/xdebug.ini

我的本​​地 docker-config 目录中的 xdebug.ini 文件如下所示:

zend_extension=xdebug.soxdebug.default_enable=1xdebug.remote_autostart=1xdebug.remote_connect_back=0xdebug.remote_port=9001xdebug.remote_enable=1xdebug.idekey=PHPSTRxdebug.remote_host = docker.for.mac.internal

在 PHPStorm 中,我设置了如下调试设置:我已经像这样设置了服务器映射:

我也像在 xdebug.ini 文件中一样将调试端口设置为 9001:

当我运行命令 php -ini |grep xdebug 我的输出如下:

root@a96543427809:/var/www/html# php -ini |grep xdebug无法加载 Xdebug - 它已被加载/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini,/usr/local/etc/php/conf.d/xdebug.ini调试xdebug 支持 =>启用xdebug.auto_trace =>关闭 =>离开xdebug.cli_color =>0 =>0xdebug.collect_assignments =>关闭 =>离开xdebug.collect_includes =>在 =>在xdebug.collect_params =>0 =>0xdebug.collect_return =>关闭 =>离开xdebug.collect_vars =>关闭 =>离开xdebug.coverage_enable =>在 =>在xdebug.default_enable =>在 =>在xdebug.dump.COOKIE =>没有价值 =>没有价值xdebug.dump.ENV =>没有价值 =>没有价值xdebug.dump.FILES =>没有价值 =>没有价值xdebug.dump.GET =>没有价值 =>没有价值xdebug.dump.POST =>没有价值 =>没有价值xdebug.dump.REQUEST =>没有价值 =>没有价值xdebug.dump.SERVER =>没有价值 =>没有价值xdebug.dump.SESSION =>没有价值 =>没有价值xdebug.dump_globals =>在 =>在xdebug.dump_once =>在 =>在xdebug.dump_undefined =>关闭 =>离开xdebug.extended_info =>在 =>在xdebug.file_link_format =>没有价值 =>没有价值xdebug.filename_format =>没有价值 =>没有价值xdebug.force_display_errors =>关闭 =>离开xdebug.force_error_reporting =>0 =>0xdebug.gc_stats_enable =>关闭 =>离开xdebug.gc_stats_output_dir =>/tmp =>/tmpxdebug.gc_stats_output_name =>gcstats.%p =>gcstats.%pxdebug.halt_level =>0 =>0xdebug.idekey =>PHPSTR =>PHPSTRxdebug.max_nesting_level =>256 =>256xdebug.max_stack_frames =>-1 =>-1xdebug.overload_var_dump =>2 =>2xdebug.profiler_aggregate =>关闭 =>离开xdebug.profiler_append =>关闭 =>离开xdebug.profiler_enable =>关闭 =>离开xdebug.profiler_enable_trigger =>关闭 =>离开xdebug.profiler_enable_trigger_value =>没有价值 =>没有价值xdebug.profiler_output_dir =>/tmp =>/tmpxdebug.profiler_output_name =>cachegrind.out.%p =>cachegrind.out.%pxdebug.remote_addr_header =>没有价值 =>没有价值xdebug.remote_autostart =>在 =>在xdebug.remote_connect_back =>关闭 =>离开xdebug.remote_cookie_expire_time =>3600 =>3600xdebug.remote_enable =>在 =>在xdebug.remote_handler =>dbgp =>数据库xdebug.remote_host =>docker.for.mac.internal =>docker.for.mac.internalxdebug.remote_log =>没有价值 =>没有价值xdebug.remote_mode =>请求 =>请求xdebug.remote_port =>9001 =>9001xdebug.remote_timeout =>200 =>200xdebug.scream =>关闭 =>离开xdebug.show_error_trace =>关闭 =>离开xdebug.show_exception_trace =>关闭 =>离开xdebug.show_local_vars =>关闭 =>离开xdebug.show_mem_delta =>关闭 =>离开xdebug.trace_enable_trigger =>关闭 =>离开xdebug.trace_enable_trigger_value =>没有价值 =>没有价值xdebug.trace_format =>0 =>0xdebug.trace_options =>0 =>0xdebug.trace_output_dir =>/tmp =>/tmpxdebug.trace_output_name =>trace.%c =>跟踪.%cxdebug.var_display_max_children =>128 =>128xdebug.var_display_max_data =>512 =>512xdebug.var_display_max_depth =>3 =>3

我已经阅读了几个教程,但一直无法弄清楚这一点.任何人都有任何可以帮助我的见解?

解决方案

我会怎么做

  • 首先你使用的是 wordpress:latest 所以你必须找到哪个 php您的 wordpress 图像正在运行的版本.
  • 然后检查容器中是否安装了xdebug.很可能不是!
  • 使用 xdebug 安装创建一个 Dockerfile 用于开发,不要将其用于生产,它会降低您的性能.
  • 从那个 Dockerfile 构建 your-wordpress 镜像.
  • 您可能需要使用远程主机详细信息创建 xdebug.ini 文件,我正在使用 phpstrom 这就是我的 xdebug.ini 的样子.我将此文件保存在我的主机中,并使用 volumes 复制它.
<块引用>

zend_extension=xdebug.soxdebug.remote_enable=1xdebug.remote_handler=dbgpxdebug.remote_port=10000 # 不要使用 9000 它通常被 php-fpm 使用xdebug.remote_autostart=1xdebug.remote_connect_back=0xdebug.idekey=PHPSTRMxdebug.remote_host=host-ip-here

这就是我的 docker-compose 文件的样子

<块引用>

版本:'2'服务:我的服务:建造:上下文:./devdockerfile: Dockerfile端口:- 8080:80"重启:总是环境:- RUNTIME_ENVIRONMENT=本地工作目录:/src卷:- ./src:/src- ./php-ini-overrides-dev.ini/xdebug.ini:/etc/php/7.1/mods-available/xdebug.ini

  • 然后使用 xdebug.ini 详细信息配置 phpstorm 远程调试.
  • 要确保加载了 xdebug 配置,请登录容器并运行 php -ini |grep xdebug
  • 这是让 xdebug 工作的一种方式.
  • 如果它不起作用,请启用 xdebug 日志,将 xdebug.remote_log=/log_path/xdebug.log 添加到 xdebug.ini 将有助于诊断问题.
  • 如果您在设置 phpstorm 远程调试方面还需要帮助,请告诉我.其实那里有很多资源.

I am trying to set up Xdebug with docker-compose. I'm using PhpStorm IDE and the Docker image I'm using installs PHP 7.2. I'm trying to do this within the context of this WordPress image. Here is my current docker-compose.yml file:

    version: '3'

    services:
   db:
     image: mysql:5.7
     ports: 
       - "3306:3306"
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   web-build-scripts:
     build: .
     depends_on:
       - db
     ports:
       - "8028:80"
       - "8029:8029"

     volumes:
       - ./themes/XXXXX:/var/www/html/wp-content/themes/XXXX
       - ./plugins/XXXX:/var/www/html/wp-content/plugins/XXXXX
       - ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX
       - ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX
       - ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX
       - ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX
       - ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX
       - ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX
       - ./plugins/XXXXX:/var/www/html/wp-content/plugins/XXXXX


     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
       VIRTUAL_HOST: leasepilot.local

volumes:
    db_data:

Then my Dockerfile which I have tagged as web-build-scripts (and built of course) looks like this:

FROM wordpress:latest

WORKDIR /var/www/html
RUN pecl install xdebug-2.6.0 
    && docker-php-ext-enable xdebug 
    && apt-get update 
    && apt-get install nano 
    && export TERM=xterm

COPY Search-Replace-DB/ ./wp-content/Search-Replace-DB/
COPY uploads/ ./wp-content/uploads/
COPY ./docker-config/xdebug.ini ../../../usr/local/etc/php/conf.d/xdebug.ini

Where my xdebug.ini file that is within my docker-config directory locally looks like so:

zend_extension=xdebug.so
xdebug.default_enable=1
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.remote_port=9001
xdebug.remote_enable=1
xdebug.idekey=PHPSTR
xdebug.remote_host = docker.for.mac.internal

Within PHPStorm I have set the debug settings like so: And I have set up the server mapping like so:

I have also set the Debug Port to 9001 as I did in my xdebug.ini file:

When I run the command php -ini | grep xdebug my output is as follows:

root@a96543427809:/var/www/html# php -ini | grep xdebug
Cannot load Xdebug - it was already loaded
/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini,
/usr/local/etc/php/conf.d/xdebug.ini
xdebug
xdebug support => enabled
xdebug.auto_trace => Off => Off
xdebug.cli_color => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_includes => On => On
xdebug.collect_params => 0 => 0
xdebug.collect_return => Off => Off
xdebug.collect_vars => Off => Off
xdebug.coverage_enable => On => On
xdebug.default_enable => On => On
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.extended_info => On => On
xdebug.file_link_format => no value => no value
xdebug.filename_format => no value => no value
xdebug.force_display_errors => Off => Off
xdebug.force_error_reporting => 0 => 0
xdebug.gc_stats_enable => Off => Off
xdebug.gc_stats_output_dir => /tmp => /tmp
xdebug.gc_stats_output_name => gcstats.%p => gcstats.%p
xdebug.halt_level => 0 => 0
xdebug.idekey => PHPSTR => PHPSTR
xdebug.max_nesting_level => 256 => 256
xdebug.max_stack_frames => -1 => -1
xdebug.overload_var_dump => 2 => 2
xdebug.profiler_aggregate => Off => Off
xdebug.profiler_append => Off => Off
xdebug.profiler_enable => Off => Off
xdebug.profiler_enable_trigger => Off => Off
xdebug.profiler_enable_trigger_value => no value => no value
xdebug.profiler_output_dir => /tmp => /tmp
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.remote_addr_header => no value => no value
xdebug.remote_autostart => On => On
xdebug.remote_connect_back => Off => Off
xdebug.remote_cookie_expire_time => 3600 => 3600
xdebug.remote_enable => On => On
xdebug.remote_handler => dbgp => dbgp
xdebug.remote_host => docker.for.mac.internal => docker.for.mac.internal
xdebug.remote_log => no value => no value
xdebug.remote_mode => req => req
xdebug.remote_port => 9001 => 9001
xdebug.remote_timeout => 200 => 200
xdebug.scream => Off => Off
xdebug.show_error_trace => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.show_mem_delta => Off => Off
xdebug.trace_enable_trigger => Off => Off
xdebug.trace_enable_trigger_value => no value => no value
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.trace_output_dir => /tmp => /tmp
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3

I've gone through several tutorials and haven't been able to figure this out. Anyone hav any insights that may help me?

解决方案

What i would do

  • First you are using wordpress:latest so you have to find which php version your wordpress image is running.
  • Then check is xdebug is installed in the container. Most probably not!
  • Create a Dockerfile with xdebug installation for the development, dont use this for production, it will slow down your performance.
  • Build your-wordpress image from that Dockerfile.
  • You might need to create the xdebug.ini file with your remote host details, i'm using phpstrom this is what my xdebug.ini looks like. I keep this file in my host machine and copy it with volumes.

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_port=10000 # dont use 9000 its usually used by the php-fpm
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.idekey=PHPSTRM
xdebug.remote_host=host-ip-here

This is how my docker-compose file looks like

version: '2'
services:
    my-service:
        build:
          context: ./dev
          dockerfile: Dockerfile
        ports:
            - "8080:80"
        restart: always
        environment:
            - RUNTIME_ENVIRONMENT=local
        working_dir: /src
        volumes:
            - ./src:/src
            - ./php-ini-overrides-dev.ini/xdebug.ini:/etc/php/7.1/mods-available/xdebug.ini

  • Then configure phpstorm remote debugging with xdebug.ini details.
  • To make sure xdebug config loaded, log into container and run php -ini | grep xdebug
  • This is one way of getting xdebug works.
  • If its not working enable xdebug log, add xdebug.remote_log=/log_path/xdebug.log to xdebug.ini will help to diagnose the issue.
  • Let me know if you also need help with setuping the phpstorm remote debugging. Actually there are lots of resources out there.

相关文章