Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 387
David Jarrin Avatar asked Jan 01 '23 16:01

David Jarrin


2 Answers

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.
like image 164
Ntwobike Avatar answered Jan 05 '23 05:01

Ntwobike


@david-jarrin,

Looking at your php -ini output, it looks like your XDebug is already running. Could it be installing (and activating) a never version, version 3.x instead of the 2.x you've specified? That was the grief in my case.

Please note that version 3 of XDebug has introduced a fair amount of config name changes that are not backwards compatible. There's an upgrade guide available. You'll find that various config keys have changed, including ones that are vital in running XDebug from a Docker container.

I'm using the official Wordpress Docker image just as you are, and it took a while to dig up the reason why suddenly xdebug.remote_host doesn't work anymore, and why my logs aren't showing up. It's all in the upgrade guide.

This is a basic config that works for me, when using VS Code with felixbecker's XDebug extension.

You can include the xdebug install in your Dockerfile. This actually runs a custom function inside the php container that the Wordpress container is using, docker-php-ext-enable. Use the Dockerfile to build your XDebug-enabled version of the Wordpress container:

FROM wordpress:latest

RUN pecl install "xdebug" \
    && docker-php-ext-enable xdebug

Create an xdebug.ini file in a folder relative to your docker-compose.yml file, like ./conf/xdebug.ini for example:

xdebug.client_host=host.docker.internal
xdebug.client_port=9000
xdebug.mode=debug
xdebug.log_level=7
xdebug.log="/tmp/xdebug.log"
xdebug.idekey=sts-debug
xdebug.max_nesting_level=1500
xdebug.connect_timeout_ms=60000

It's more flexible to inject the xdebug config like this in your docker-compose.yml file, that way you don't have to rebuild your image every time you change your xdebug config.

wordpress:
    build: .
    depends_on:
      - db
    image: custom-wordpress:latest
    ports:
      - '8000:80'
    restart: unless-stopped
    volumes:
      - './:/var/www/html'
      - ./conf/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini

Happy trails!

like image 37
lauri108 Avatar answered Jan 05 '23 03:01

lauri108