Getting PHPStorm working with XDEBUG and Docker to debug magento

Posted on Wed 03 January 2018 in misc

PHPStorm Settings:

phpstorm_xdebug_1.png phpstorm_xdebug_2.png

I have duplicated the site to have one for port 80 and one for 443 (cover all bases here as we are running our test site on HTTPS)

Remember to set PHPStorm to listen on port 9001 (default is 9000) but php itself is running on 9000 and could conflict

Chrome Xdebug plugin Xdebug Plugin from Chrome store Right click on this and setup, make sure you select PHPSTORM ase your IDE

Docker settings: Before running docker-compose up -d Export the environment variables to your terminal

export XDEBUG_CONFIG="remote_enable=1 remote_host=docker.for.mac.localhost remote_port=9001 idekey=PHPSTORM

To check that the docker settings are correct:

(Some items removed as they had keys etc in

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 ~/GIT/absolutemusic/dev   ABS-589 ●  docker exec -it absolutemusic_php_1 /bin/bash
root@php-fpm-local:/var/www/html# env
HOSTNAME=php-fpm-local
TERM=xterm
PHP_INI_DIR=/usr/local/etc/php
PHP_FILENAME=php-5.5.38.tar.xz
MAGE_ROOT_DIR=/var/www/html
PHPIZE_DEPS=autoconf        file        g++         gcc         libc-dev        make        pkg-config      re2c
PHP_EXT_MEMCACHED_VERSION=2.2.0
PHP_EXT_APCU_VERSION=4.0.11
XDEBUG_CONFIG=remote_enable=1 remote_host=docker.for.mac.localhost remote_port=9001 idekey=PHPSTORM
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

ENABLE_SENDMAIL=true
PWD=/var/www/html
ION_CUBE_PHP_VERSION=5.5
SHLVL=1
HOME=/root

PHP_VERSION=5.5.38
PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data
_=/usr/bin/env
root@php-fpm-local:/var/www/html#

Putting it all together

1
2
3
4
cd <directory_containing_docker-compose.yml>
export XDEBUG_CONFIG="remote_enable=1 remote_host=docker.for.mac.localhost remote_port=9001 idekey=PHPSTORM
docker-compose up -d
<clear cache of site>

If this give issues, restart the php container, clear the cache and make sure you have some breakpoints

Other notes:

the remote_host=docker.for.mac.localhost is a special dns name docker provides to allow the containers to communicate with the host. This appears to only be needed in docker for mac.

1
2
3
4
5
docker exec -it absolutemusic_php_1 /bin/bash
root@php-fpm-local:/var/www/html# ping docker.for.mac.localhost
PING docker.for.mac.localhost (192.168.65.1): 56 data bytes
64 bytes from 192.168.65.1: icmp_seq=0 ttl=37 time=0.362 ms
64 bytes from 192.168.65.1: icmp_seq=1 ttl=37 time=1.364 ms

Logging on to the php container and pinging this ip resolved it to match the subnet in the docker for mac config.

Checkingn XDEBUG Settings in the containers:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
docker-compose exec php php -i | grep xdebug
/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini,
/usr/local/etc/php/conf.d/zz-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.force_display_errors => Off => Off
xdebug.force_error_reporting => 0 => 0
xdebug.halt_level => 0 => 0
xdebug.idekey => PHPSTORM 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 => Off => Off
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.localhost => docker.for.mac.localhost
xdebug.remote_log => no value %


#!bash
docker-compose exec php cat /usr/local/etc/php/conf.d/zz-xdebug.ini
xdebug.remote_enable=1
xdebug.remote_host=docker.for.mac.localhost
xdebug.remote_port=9001
xdebug.idekey=PHPSTORM