PHPStorm doesn't work with xdebug + docker, but is ok without docker

Hi.

I'm on ubuntu 20, runing the latest stable PHPStorm. I'm working on a project running on PHP 5.6, with cakePHP.

I can run the project on a local apache+php5.6+xdebug2.5.5 and it works fine. When I set up PHP>Debug and validate my configuration for xdebug, it is ok.When I enable Chrome xdebug extension and start listening for debug connections in Storm, my breakpoint (at the very start of the page execution) is catched and I can debug.

Now, when I run the exact same project with Docker, everything works fine but my breakpoint doesn't trigger anything. It is placed at the exact same line.

My container has the following setting:
extra_hosts:
  - host.docker.internal:host-gateway

I compared the xdebug configuration in phpinfo between my local server and the container, both are strictly identical (except for remote_host, it's localhost on my local server, and the log paths).

When I try to validate my debug configuration in Storm for the docker version, host.docker.internal is found and everything is ok.

I run Docker in rootless mode but when I go to Build, Execution, Deployment>Docker and configure my Docker, the connection is successful.

Here is my xdebug config on Docker container:

xdebug

xdebug support enabled
Version 2.5.5
IDE Key PHPSTORM
Supported protocols Revision
DBGp - Common DeBuGger Protocol $Revision: 1.145 $
Directive Local Value Master Value
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 phpstorm://open?%f:%l phpstorm://open?%f:%l
xdebug.force_display_errors Off Off
xdebug.force_error_reporting 0 0
xdebug.halt_level 0 0
xdebug.idekey PHPSTORM PHPSTORM
xdebug.max_nesting_level 512 512
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 /var/www/data/debug/profiles /var/www/data/debug/profiles
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 host.docker.internal host.docker.internal
xdebug.remote_log no value no value
xdebug.remote_mode req req
xdebug.remote_port 9000 9000
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 /var/www/data/debug/traces /var/www/data/debug/traces
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 don't know where to look at. Given that xdebug configs are almost identical between my local server and my Docker container, that Storm debug validation is successful on both servers, that my Storm for the project in Docker catches the xdebug connection when I load the project on my local server (although as an external connection since it's not from the Docker server address), I'm guessing something is lost somewhere inside Docker but I'm not really sure anymore.

Any ideas?

Regards

评论操作 固定链接

debug validation for Docker successful

0
评论操作 固定链接

Hi there,

Enable Xdebug log (restart php/Apache so it sees the new config), try to debug and see what it has to say about it (if Xdebug is connecting and where / if at all).

If Xdebug log is empty/not created: 1) check the path (use absolute path) 2) check file permissions 3) make sure that Xdebug is actually trying to debug (e.g. have xdebug.remote_autostart=1 or xdebug_break(); in PHP code)

 

debug validation for Docker successful

It just checks the basic settings (that they are present / basic checks). It does not check the actual connectivity (if it can make a connection to the IDE).

 

P.S. In your case it may not bee needed, but I would switch from default 9000 port to some another (e.g. Xdebug 3 uses 9003) anyway -- to avoid possible issues with php-fpm that also uses that port.

0
评论操作 固定链接

Hi, thanks for helping.

So I set xdebug.remote_enable=1, xdebug.remote_autostart=1, define xdebug.remote_log=/tmp/xdebug. Also I changed the port to 9003.

And I get absolutely no log :(

At first I tried to set the logpath to /var/www/xdebug but I thought maybe there's a permission problem, so I tried /tmp/xdebug but it doesn't work either.

I also tried to place a xdebug_break(); in the code. I'm sure it went through it as it was right before my phpinfo(); which appears on screen.

I'm a bit confused.

0
评论操作 固定链接

so I tried /tmp/xdebug but it doesn't work either.

1. Can your PHP script write there (e.g. using fwrite() or alike)? To check that it can write there...

2. I do not think you have SELinux or alike in Docker containers .. but going to mention it anyway ( sudo setsebool -P httpd_can_network_connect=1 ). In those threads where I was suggesting it (and that was ordinary system, not virtual) it was helping. From https://intellij-support.jetbrains.com/hc/en-us/community/posts/206339279/comments/206899645

3. Check this one (not sure if it will help with container though) -- https://stackoverflow.com/a/63358357/783119

You need Xdebug log to confirm what it is doing.

P.S. Too bad you have to stick to Xdebug v2 -- v3 has easier ways of troubleshooting.

I also tried to place a xdebug_break(); in the code. I'm sure it went through it as it was right before my phpinfo(); which appears on screen.

Always add some unique code that can confirm that it is run on the right system. E.g. print IP or server name etc (something unique that will allow you to differentiate one system from another). That's in case if you are executing the wrong URL or file was not updated there. Just a random thought that helped me in the past.

I run Docker in rootless mode...

This might be related.

This guy is having a similar sort of issue. The mention of "rootless docker" is his last comment. https://stackoverflow.com/questions/70577709/xdebug-3-couldnt-connect-from-docker-20-10-12-to-phpstorm-debug-client-ubunt

No better suggestions from me right now.

0
评论操作 固定链接

Thanks for the links. He mentions rootless docker but at least he seems to have some logs.

In my script I did some file_put_contents() and some fwrite() to /tmp, everything seems fine but in CLI, in my container I can't find the files that are supposed to have been created...

edit even before posting :D :

I can't see these files but as writing doesn't seem to be a problem, I took a wild guess and tried accessing my xdebug log and it worked!! it says:

Log opened at 2022-11-18 15:16:50
I: Connecting to configured address/port: host.docker.internal:9003.
W: Creating socket for 'host.docker.internal:9003', poll success, but error: Operation now in progress (29).
E: Could not connect to client. :-(
Log closed at 2022-11-18 15:16:50

Log opened at 2022-11-18 15:16:51
I: Connecting to configured address/port: host.docker.internal:9003.
W: Creating socket for 'host.docker.internal:9003', poll success, but error: Operation now in progress (29).
E: Could not conn'...

I thought there was a problem with local port 9003 so I tried to switch the xdebug of my non-dockerized project to 9003 and also set Storm to listen to 9003 only, and the break happens. So it's not a 9003 thing.

At least we keep going! thanks

0
评论操作 固定链接

TBH I do not have much concrete ideas (as I'm on Windows, rarely work with Docker and when I do it just works in those images).

Right now it looks like Xdebug cannot make an outgoing connection for some reason.

  • Could be how rootless Docker deals with networking (cannot help here at all)
  • Some kind of firewall / networking rule
  • Could be IPv6 vs Pv4 as well (try sticking to IPv4 to check, see to which IP host.docker.internal is resolved)
  • Try using numeric IP instead of host.docker.internal (in case it may point to the wrong address, although you have added the extra_hosts line) -- see ip a kind of output (or whatever you have there)
  • Maybe your Docker image has some bad bindings (e.g. a common mistake is to port forward/expose Xdebug port as if it's an incoming connection).
  • Are you able to make outgoing connection from inside the docker to your host OS (e.g. call some simple URL on your local test web server or alike -- this kind of thing)
  • Make sure that PhpStorm is the process that listen on Xdebug port (on your host OS) and not any other process (unlikely to be an issue here since the local debug works)
0
评论操作 固定链接

I disabled my firewall and host.docker.internal is resolved to my ipv4 address.

Actually I think the issue is about your first point: rootless docker. I found this: https://www.reddit.com/r/docker/comments/vx828k/comment/ifuttc3/ which pretty seems like my problem and lead me to this https://github.com/moby/moby/issues/41904#issuecomment-765144432.

Now it says something about slirp4netns which I don't know what it is. And honestly I'm not a docker or even network genius, so I think I might convene with some coworkers who might be willing to go the next step and figure this out.

I will try to remember this post and update it if things improve.

Anyway, thank you very much for your help, you provided me with a big lead!

Regards

0
评论操作 固定链接

https://stackoverflow.com/q/72402715/783119

Try with the IP address of the host OS (e.g. xdebug.client_host=192.168.178.46 )

0
评论操作 固定链接

Ahah it works!

So I removed a lot of things I added, and the important settings are xdebug.remote_host and xdebug.remote_connect_back=0

xdebug config is now:

xdebug

xdebug support enabled
Version 2.4.0
IDE Key PHPSTORM
Supported protocols Revision
DBGp - Common DeBuGger Protocol $Revision: 1.145 $
Directive Local Value Master Value
xdebug.auto_trace Off Off
xdebug.cli_color 0 0
xdebug.collect_assignments Off Off
xdebug.collect_includes On On
xdebug.collect_params 1 1
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 no value no value
xdebug.max_nesting_level 400 400
xdebug.max_stack_frames -1 -1
xdebug.overload_var_dump On On
xdebug.profiler_aggregate Off Off
xdebug.profiler_append Off Off
xdebug.profiler_enable Off Off
xdebug.profiler_enable_trigger On On
xdebug.profiler_enable_trigger_value no value no value
xdebug.profiler_output_dir /var/www/data/debug/profiles /var/www/data/debug/profiles
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 192.168.0.21 192.168.0.21
xdebug.remote_log no value no value
xdebug.remote_mode req req
xdebug.remote_port 9000 9000
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 On On
xdebug.trace_enable_trigger On On
xdebug.trace_enable_trigger_value no value no value
xdebug.trace_format 0 0
xdebug.trace_options 0 0
xdebug.trace_output_dir /var/www/data/debug/traces /var/www/data/debug/traces
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

On Storm side, I ended up having no configuration to do except setting up a local server (PHP/Servers) with host localhost and port 9999, and mapping the project root path to /var/www

And when I open http://localhost:9999 my breakpoints are working!

Thanks a bunch for your precious help!

1

请先登录再写评论。