Docker4drupal: XDebug for Drush

Created on 18 Apr 2017  路  16Comments  路  Source: wodby/docker4drupal

XDebug for http requests in the browser is working OK, I just wonder how I could get XDebug to also work for Drush on the console.

In PhpStorm this works quite nicely when Drush is executed on the host. What it does is to execute something like this:

/usr/bin/php -dzend_extension=/usr/lib/php/20151012/xdebug.so -dxdebug.remote_enable=1 -dxdebug.remote_mode=req -dxdebug.remote_port=9000 -dxdebug.remote_host=127.0.0.1 /var/www/html/vendor/drush/drush/drush status

Abd I can set breakpoints like always and everything is working just fine. However, if Drush is executed inside the PHP container, I don't get this to work and wonder what else I need to be doing.

Any idea?

Most helpful comment

@rafatwork that's because drush is located out of codebase dir, so PhpStorm fails to map php execution to php sources. The best solution is per project drush. Add drush as dependency via composer and use it via ./vendor/bin/drush/drush/drush or drush symlink in the root of the project.

All 16 comments

I use the following configuration which works in any cases:

PHP_XDEBUG=1
PHP_XDEBUG_DEFAULT_ENABLE=1
PHP_XDEBUG_REMOTE_HOST=<docker0 IP or 127.0.0.1>
PHP_IDE_CONFIG=serverName=<Server name from PhpStorm (see attached screenshot)>
PHP_XDEBUG_REMOTE_CONNECT_BACK=0

image

That's the configuration for debugging web sessions in the browser, but that doesn't help for debugging Drush commands, does it?

That works for me both for drush/composer and web requests. Also try to enable "Break at the first line in PHP scripts" and execute drush command.

Hmm, no luck with it yet.
My docker compose config looks like this:

      PHP_XDEBUG: 1
      PHP_XDEBUG_DEFAULT_ENABLE: 1
      PHP_XDEBUG_REMOTE_AUTOSTART: 1
      PHP_XDEBUG_REMOTE_HOST: 127.0.0.1
      PHP_IDE_CONFIG: Docker
      PHP_XDEBUG_REMOTE_CONNECT_BACK: 1

Note, I had to change the last piece to 1, otherwise I wouldn't even get web sessions to work.

My server setting in PhpStorm is this:

2017-04-26 12 58 19 screen

Again, web server debugging is working but when I go into the PHP container with docker-compose exec php sh and execute Drush there, I don't get any response in PhpStorm.

You should use PHP_IDE_CONFIG=serverName=Docker instead of PHP_IDE_CONFIG: Docker.

Also, I'm not sure about PHP_XDEBUG_REMOTE_HOST: 127.0.0.1. Try to use your local network IP address or docker0 IP address or share output of the ifconfig command I'll help you to detect a correct one. Btw, what is your OS? Ubuntu?

OK about the PHP_IDE_CONFIG, but the first = should be a colon, right?

My ifconfig of the PHP container:

eth0      Link encap:Ethernet  HWaddr 02:42:AC:12:00:0A  
          inet addr:172.18.0.10  Bcast:0.0.0.0  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2282 errors:0 dropped:0 overruns:0 frame:0
          TX packets:2983 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:13758223 (13.1 MiB)  TX bytes:823207 (803.9 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:64 errors:0 dropped:0 overruns:0 frame:0
          TX packets:64 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:4268 (4.1 KiB)  TX bytes:4268 (4.1 KiB)

Yes, my OS is Ubuntu 16.04

OK about the PHP_IDE_CONFIG, but the first = should be a colon, right?

yep

Try to define PHP_XDEBUG_REMOTE_HOST: 172.18.0.10. Btw it's strange why you don't have docker0 network intarface.

Problem is, when I set 172.18.0.10 in the docker-compose.yml and restart the container, then the IP address changes, it's now 172.18.0.11

My docker-compose.yml looks like this:

version: "2"

services:
  mariadb:
    image: wodby/mariadb:10.1-2.0.0
    ports:
      - '8306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: drupal
      MYSQL_USER: drupal
      MYSQL_PASSWORD: drupal

  php:
    image: wodby/drupal-php:7.0-2.1.0
    environment:
      PHP_XDEBUG: 1
      PHP_XDEBUG_DEFAULT_ENABLE: 1
      PHP_XDEBUG_REMOTE_AUTOSTART: 1
      PHP_XDEBUG_REMOTE_HOST: 172.18.0.10
      PHP_IDE_CONFIG: serverName=Docker
      PHP_XDEBUG_REMOTE_CONNECT_BACK: 1
    volumes:
      - ./:/var/www/html

  nginx:
    image: wodby/drupal-nginx:8-1.10-2.0.0
    restart: unless-stopped
    depends_on:
      - php
    environment:
      NGINX_BACKEND_HOST: php
      NGINX_SERVER_ROOT: /var/www/html/web/
    volumes:
      - ./:/var/www/html
    labels:
      - 'traefik.backend=knimed8_nginx_1'
      - 'traefik.port=80'
      - 'traefik.frontend.rule=Host:knimed8.docker.localhost'

The Traefik container is running on its own, because that serves multiple projects at the time.

172.18.0.10 is a in-container IP, you have to exec ifconfig on your host machine and take the IP address of the docker0 interface.

Yeah, that's it! Thanks a lot @PavelPrischepa this is now solved. Great support.

Experiencing the exact same issue on my installation and just can't get it to work. Browser debugging works fine.

Could someone please give me a complete rundown/screenshots of how phpStorm should be configured to get this to work?

Getting the following error in phpStorm whenever "Listening for PHP Debug connections" is turned on & running any drush command:
Remote file path '/home/www-data/.composer/vendor/drush/drush/drush' is not mapped to any file path in project

@rafatwork that's because drush is located out of codebase dir, so PhpStorm fails to map php execution to php sources. The best solution is per project drush. Add drush as dependency via composer and use it via ./vendor/bin/drush/drush/drush or drush symlink in the root of the project.

@PavelPrischepa Thanks, I'll give that a go ASAP. What about Drupal 7 projects however? There's no composer involved.

@rafatwork it seems the same issue, drush installed globally that's why its sources isn't available for PhpStorm. So in case of D7 just put drush into the codebase.

@PavelPrischepa
Got this to work by the way - thanks a lot.
Is there also a way to make it work without enabling "Break at the first line in PHP scripts"?

Is there also a way to make it work without enabling "Break at the first line in PHP scripts"?

@rafatwork you should configure directory mapping in Languages & Frameworks -> PHP -> Path mappings.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

webdobe picture webdobe  路  5Comments

dcre picture dcre  路  6Comments

magick93 picture magick93  路  7Comments

Vacilando picture Vacilando  路  9Comments

Vacilando picture Vacilando  路  7Comments