Xdebug with SSH tunnel on Docker for Mac

I recently read a lot of posts from the Docker community on how to debug a PHP application in PHPStorm using Docker for Mac. All of them contain pieces of useful information, but in one place there was no working solution.

+1
source share
2 answers

Here is what worked for me.

Inner Dock Container

Change xdebug configuration

# automatically start debugger on every request xdebug.remote_enable=1 xdebug.remote_autostart=1 xdebug.remote_port=9000 # send all debug requests to 127.0.0.1, remote_connect_back should be turned off xdebug.remote_connect_back = 0 xdebug.remote_host=127.0.0.1 #log all xdebug requests to see is it working correctly xdebug.remote_log=/var/log/remote.log 

Make sure xdebug is working

At this point, try starting the PHP application. The log must contain such entries for each request:

I: Connecting to configured address/port: 127.0.0.1:9000 I: Connected to client. :-)

If you see something like this in the log, the remote_connect remote_connect or remote_connect_back files are not configured correctly.

I: Checking remote connect back address. I: Checking header 'HTTP_X_FORWARDED_FOR'. I: Checking header 'REMOTE_ADDR'. I: Remote address found, connecting to 172.18.0.1:9000. W: Creating socket for '172.18.0.1:9000', poll: Operation now in progress. E: Could not connect to client. :-(

I saw situations when Xdebug worked in the CLI, but not from the browser, when this problem appeared in the log, remote_connect_back = 0 fixed it.

Sshd configuration

To enable ssh tunneling in a container: edit / etc / ssh / sshd _conf and add:

GatewayPorts yes

Restart sshd if necessary (ideally this should be part of the Docker file).

On the host computer

Start reverse ssh tunnel

Run this command and open it on a separate terminal tab: ssh -p {container_22_port} -R 9000:localhost:1111 root@127.0.0.1

where {container_22_port} is the port on the host that is mapped to port 22 on the docker container. 9000 is the port used by Xdebug inside the container, port 1111, which the host will use to listen for Xdebug connections.

Test with netcat

At this point, you can verify that Xdebug actually transfers information from the docker container to the host machine. Run netcat to find out what is sent on port 1111 and run the php application:

nc -l 1111

You should see something like this:

 <?xml version="1.0" encoding="iso-8859-1"?> <init xmlns="urn:debugger_protocol_v1" xmlns:xdebug="http://xdebug.org/dbgp/xdebug" fileuri="file:///var/www/magento2/index.php" language="PHP" xdebug:language_version="7.0.12" protocol_version="1.0" appid="1006" idekey="XDEBUG_ECLIPSE"><engine version="2.5.0rc1"><![CDATA[Xdebug]]></engine><author><![CDATA[Derick Rethans]]></author><url><![CDATA[http://xdebug.org]]></url><copyright><![CDATA[Copyright (c) 2002-2016 by Derick Rethans]]></copyright></init> 

Configure PhpStorm

Opne File DefaultSettings , and there in Languages&Frameworks PHP Debug change Xdebug Debug port to 1111 (the one we used to open the ssh tunnel). PhpStorm should start accepting connections from xdebug at this point.

Are there any problems with this approach?

+5
source

I found out that you do not need an SSH tunnel to make it work.

Xdebug only needs to connect to a working IDE debugger, but there may be some default ports that were already used as one for FPM (9000).

Inside the container

Set the xdebug options as follows:

 xdebug.remote_enable = 1 xdebug.remote_host = localhost xdebug.remote_port = 10000 xdebug.remote_connect_back = 1 

Note. If you use the nginx-proxy container as the reverse proxy, set remote_host as the one you defined in VIRTUAL_HOST

Using PhpStorm

  • File β†’ Settings
    • Languages ​​and Frames
      • Php
        • Servers: add one and set the host as localhost: 80 with Xdebug with the selected path display ... map the INSIDE the Docker folder (/ var / www /) to the one
        • Debugging: set Xdebug port to 10000 using marked external connections
0
source

Source: https://habr.com/ru/post/1233353/


All Articles