Search code examples
phpdockerserverxdebugssh-tunnel

Xdebug Remote Debugging inside Docker on remote server


I need to debug PHP scripts located inside a Docker container on a remote server.

Usually, I debug my code using Visual Studio Code in Windows. I've been debugging my dev codes (which is located in a Vagrant) this way without any problem.

But when it comes to debugging files in a Docker on a remote server I couldn't get it to work.

Here's my xdebug setting on php.ini (in docker located on a remote server)

xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host=172.18.0.1
xdebug.remote_port=9000
xdebug.remote_log=/tmp/debug.log

The remote_host IP address in the setting above is the IP of the remote host seen from inside Docker.

I've tried ssh-tunneling to my remote server (from Windows, using telnet).

But when I try to run the site on my browser, my Visual Studio Code couldn't detect the debugging session.

Basically what I did are these:

  1. Start a debugging session on my local machine (Windows)
  2. Create ssh-tunnel to remote server (forwarding port 9000 on the server to localhost:9000) using ssh -R 9000:localhost:9000 username@myremoteserver.com
  3. Mapped port 9000 on Docker to port 9000 on remote server
  4. Xdebug in docker is set to listen to its host (so to IP address of host seen from inside docker, which I got from running this command from inside docker: /sbin/ip route|awk '/default/ { print $3 }'

When I run the site, the /tmp/debug.log contains this:

I: Connecting to configured address/port: 172.18.0.1:9000.
I: Connected to client. :-)
-> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///magento/test.php" language="PHP" xdebug:language_version="5.6.33-1+ubuntu16.04.1+deb.sury.org+1" protocol_version="1.0" appid="12776" idekey="www-data"><engine version="2.5.5"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2017 by Derick Rethans]]></copyright></init>

-> <response xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" status="stopping" reason="ok"></response>

How can I 'relay' the connection set by Xdebug from Docker, to my remote server, to the IDE on my local pc?


Solution

  • I have no success to do the same by ssh-tunnel :(

    But it's possible by ngrok. You should:

    1. install ngrok on your local development machine;
    2. run ngrok tcp 9000;
    3. set xdebug.remote_port and xdebug.remote_host according to ngrok's info.