Search code examples
phpdockerdocker-composexdebug

Setting up Xdebug with Docker Compose and WordPress image


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: enter image description here And I have set up the server mapping like so: enter image description here

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

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?


Solution

  • 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.