Answer the question
In order to leave comments, you need to log in
How to connect to Xdebug running in a Docker container on a remote machine?
Colleagues, I ask for help in debugging the connection from my local machine to a remote xdebug server.
I have a development server (hereinafter referred to as host.dev) on which PHP-FPM with xdebug is running in a Docker container on Linux Alpine OS. Since the standard xdebug port 9000 is reserved for PHP-FPM, port 9001 has been allocated for xdebug .
Versions
PHP 7.1.0 (cli) (built: Dec 27 2016 19:41:35) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.1.0, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.5.1-dev, Copyright (c) 2002-2016, by Derick Rethans
; NOTE: The actual debug.so extention is NOT SET HERE but rather (/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini)
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.remote_connect_back=0
;xdebug.cli_color=0
xdebug.profiler_enable=0
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_port=9001
xdebug.remote_host=localhost
xdebug.idekey=PHPSTORM
xdebug.remote_log="/tmp/xdebug.log"
php:
build:
context: ./docker/containers/php
dockerfile: Dockerfile-dev
volumes:
- ./:/var/www/html
- ./docker/containers/php/config/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini
ports:
- "9001:9001"
ssh -R 9001:localhost:9001 [email protected]
Log opened at 2017-01-29 11:22:36
I: Connecting to configured address/port: localhost:9001.
W: Creating socket for 'localhost:9001', poll success, but error: Operation in progress (29).
W: Creating socket for 'localhost:9001', poll success, but error: Operation in progress (29).
E: Could not connect to client. :-(
Log closed at 2017-01-29 11:22:36
Answer the question
In order to leave comments, you need to log in
Here is a detailed answer to my question.
Consider the situation from the side of a remote server running a docker container.
Docker has already taken care of most of the problem and made all the routes so that you could ping the eth0 interface from the container, through which the Internet should go to the server machine. Moreover, from the container you can ping any host on the docker host, except for 127.0.0.1 of the host itself - for obvious reasons (127.0.0.1 from the container will be the lo interface of the container itself).
Let's say the IP on eth0 of your host is 10.45.6.87. That is, the ping from the docker container to 10.45.6.87 should be successful.
And since eth0 is an exit to the outside world, then you can tell xdebug that the client will be on host 10.45.6.87 port 9000, let him wait there. We will then make an SSH tunnel there.
We create a tunnel.
We forward the remote port through the SSH tunnel to the local machine
ssh -R 9000:localhost:9000 [email protected]
and expect that in the end the bind on the remote side will look like this,
10.45.6.87:9000 => 127.0.0.1:9000
but we are very wrong :)
And this mistake costs us a lot of time.
Let's see what netstat -plnt tells us .
And he should say that after starting the tunnel, on the remote machine we have a bind at
127.0.0.1:9000 , and not 10.45.6.87:9000 as expected.
That is, it turns out that xdebug is waiting for a client at 10.45.6.87:9000, and after opening the tunnel, the client is at 127.0.0.1:9000 .
The trouble is that we can only bind the client to the local host .
That's what it says in the man ssh doc .
From the container, we cannot see 127.0.0.1:9000 of the docker host, which means that it remains only to route packets from 10.45.6.87:9000 to 127.0.0.1:9000 inside the remote machine.
This can be done in many ways, including iptables. Personally, I made another SSH tunnel inside my server like this:
1. In the xdebug configs, specify
xdebug.remote_port = 9001
xdebuh.remote_host = 10.45.6.87
ssh -R 9000:127.0.0.1:9000 [email protected]
ssh -g -L 9001:localhost:9000 -f -N 127.0.0.1
xdebug.remote_port = 9000
xdebug.remote_host = <IP локальной машины>
xdebug.remote_connect_back=1
xdebug.remote_port = 9000
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question