Docker Community Forums

Share and learn in the Docker community.

[solved] How to setup xdebug and phpstorm


(Krodelabestiole) #1

Hello,

I’m a bit confused using xdebug, docker for windows, and phpstorm…

i have xdebug configured in a container with PHP.
Here is what appears in my php.ini from within this container :

xdebug.remote_enable=on
xdebug.remote_autostart=off
xdebug.idekey=PHPSTORM
xdebug.remote_connect_back=1
xdebug.remote_host={{some IP address}}

from this container i can’t access directly my Windows host IP, but i can access two IPs :
the docker host IP which is 172.17.0.1
and the docker IP i have from Windows (resolving docker) which is 10.0.75.2
why is there so many IPs ? what are there referring to exactly ?
i even can ping 10.0.75.1 which might be some virtual bridge of some kind ?

xdebug communicates on port 9000. i have bind port 9000 to 9000 for this container :

ports:
        - "80:80"
        - "9000:9000"

from my understanding this binds port 9000 from this container to port 9000 on 10.0.75.2 (docker from windows) ?
then what IP should i use for xdebug.remote_host ?
by default PHPStorm waits for some connection on port 9000, on local machine (Windows).
should I set what it calls a DBGp Proxy ? Pointing to docker ? On port 9000 ?

for now i have tried xdebug.remote_host with 172.17.0.1 , 10.0.75.2 or localhost and could not get it to work yet.


Xdebug doen't connect: Virtualhosts with Apache2 and PHP-FPM
(Krodelabestiole) #2

i’ve understood that xdebug.remote_connect_back bypass xdebug.remote_host setting, and set as a remote the http client machine. i guess then it should work in any case.
still, no luck either with or without DBGp Proxy set on host ‘docker’ and port 9000…

previously i had xdebug working with docker machine so i guess other settings are correct, but docker’s port 9000 was also bound to the Windows’ port 9000 using virtual box, then i guess it was considered as a local xdebug server by PHPStorm.


(Krodelabestiole) #4

i think i have a port issue :

my Windows IP as seen by docker seems to be 10.0.75.1 (PHP shows 10.0.75.1 for $_SERVER[‘REMOTE_ADDR’])
PHPStorm is listening to xdebug on port 9000.
therefore, when I telnet 10.0.75.1 9000 from windows it connects successfully when PHPStorm is listening and returns this error when it is not listening : “Could not open connection to the host on port 9000 : connect failed” . This makes perfect sense.
My web container is defined like this in docker-compose.yml :

web:
    build: php5
    ports:
        - "80:80"
        - "9000:9000"

And PHP shows 10.0.75.1 for $_SERVER[‘REMOTE_ADDR’] . From this web container I can ping and telnet successfully 10.0.75.1 on port 80 but on port 9000 it returns an error whether PHPStorm is listening or not :

root@fd60276df273:/var/www/html# telnet 10.0.75.1 9000
Trying 10.0.75.1...
telnet: Unable to connect to remote host: Connection timed out

Windows firewall is off. Do you know what could cause this issue ?


(Krodelabestiole) #5

hello

i think i’ve managed to strip my question down to the docker-for-windows core with this last post, so i would greatly appreciate to get an answer from you docker’s gurus.

please.
thanks.


(Michael Friis) #6

I don’t know anything about xdebug but I gather that you’re trying to connect from a container that’s running in Docker for Windows to an app that’s running and listening on a TCP port directly on the Windows 10 host?

That’s not really a supported scenario.

Having said that, there’s no reason it should not work. 10.0.75.1 is probably the IP of your Windows host on the DockerNAT network (it is for me).

If I run a webserver on the Windows 10 host and make sure that it listens for external connections (listening on locahost will probably not cut it) and completely disable Windows Firewall, I can curl the webserver from a linux container running in Docker for Windows:

curl 10.0.75.1:5000
Hello from ASP.NET Core!

Note that this is not supported and that the networking setup may vary between machines and may change in the future.


(Krodelabestiole) #7

Thanks a lot for your answer :slight_smile:

I gather that you’re trying to connect from a container that’s running in Docker for Windows to an app that’s running and listening on a TCP port directly on the Windows 10 host?

That’s it.

That’s not really a supported scenario.

Really ? Why not ?

Anyway, I’ve just tried to telnet Windows from another computer on my local network, and telnet 192.168.1.4 9000 (which is my Windows IP) connects successfully. So I guess the problem I have is somehow inherent to docker or DockerNAT. Once again, i guess my firewall is completely disabled (at least it’s shown as turned off).
Should I change any setting on this DockerNAT virtual network interface ? I can’t see any way to open any port…

I’ve also tried to change xdebug port to some random other number and it doesn’t change anything…


(Krodelabestiole) #8

should i open a bug report or something ?

i think a docker container should really be able to connect to a listening service on windows.
xdebug is a major tool for web developers, who i guess are a big part of docker users. and i guess there are a bunch of other uses where this would be requested.


(Michael Friis) #9

You can join the fray here: https://github.com/docker/docker/issues/1143

Workarounds:


(Krodelabestiole) #10

thanks Michael !

though this threads are about using a host name to access host from a container. in this case i don’t want to resolve any IP, i guess i know my host IP and i would not even need it with xdebug.remote_connect_back.
all of these people don’t seem to have any issue to access a service on the host machine, and i had no issue either when i was using docker-machine - i just had to open a port on virtualbox.

i haven’t understood this solution :
http://blog.javabien.net/2014/06/12/give-a-docker-container-full-access-to-hosts-network/
i use docker-compose and “docker run --net” option doesn’t seem to exist anyway…

i’ve updated to the latest build, 1.12.0-rc2-beta16 and still no luck.
i’ve also tried to use the option “Expose container ports on localhost” but port 9000 then appears busy if it is bound for the container, and is still not accessible from the container anyway.


(Krodelabestiole) #11

ok i finally found this doc about networking : https://docs.docker.com/compose/networking/

and used option
network_mode: “host” in docker-compose

i can telnet 192.168.65.1 9000 ! i don’t know where this ip comes from (i found it with ifconfig) but it works when xdebug is listening, and not otherwise !

i will post more info soon.


(Krodelabestiole) #12

so here is how i got it to work

docker-compose.yml :

version: '2'

services:
    web:
        build: php5
        ports:
            - "80:80"
        #links:
        #    - db:db
        network_mode: "host"
    db:
        image: mysql
        ports:
            - "3306:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=root

php.ini :

xdebug.idekey=PHPSTORM
xdebug.remote_port=9000
xdebug.remote_host=192.168.65.1

i selected “Expose container ports on localhost” as well in docker settings.
i can’t use links any more, because of the specified network_mode. so i’ve opened port 3306 and i have to choose 192.168.65.1 as mysql host. i will probably find some workaround about this, but finally it works ! im glad ! thanks Michael for having pointed this --net option.


(Visual4) #13

xdebug needs to connect back to your windows machine (PHPSTORM) on port 9000, so if networking in the container is working (should work if you have http and mysql up and running), you need to find out the windows IP on the docker-windows internal net and use this as xdebug.remote_host.

Or you use xdebug.remote_connect_back without specifying an IP at all - which works fine for me in conjunction with PHPStorm’s “Start Listening for PHP Debug connections”

Cheers,
Björn


(Krodelabestiole) #14

are you sure you are using docker for windows beta ?
because i’ve been trying to make it work for weeks without success : i could not access port 9000 on windows from the container, even through i had all networking ok, apt-get and all. it was not working with any windows IP i could get, neither with xdebug.remote_connect_back…
until i tried network_mode: “host” for the container, and still, xdebug.remote_connect_back doesnt work i have to use xdebug.remote_host=192.168.65.1


(Visual4) #15

yes I am :-). Installed yesterday and imported data from my virtualbox instance.

1.12.0-rc2-beta16 with alpine linux on Hyper-V

this is part of my Dockerfile:
RUN echo “date.timezone= Europe/Paris” >> /etc/php5/fpm/php.ini
RUN touch /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “zend_extension = xdebug.so” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “xdebug.remote_enable = 1” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “xdebug.remote_connect_back = 1” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “xdebug.max_nesting_level = 1000” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “xdebug.profiler_enable_trigger = 1” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “xdebug.profiler_output_dir = “/var/log”” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “memory_limit = 512M” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “upload_max_filesize = 512M” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “post_max_size = 512M” >> /etc/php5/fpm/conf.d/40-custom.ini

Cheers,
Björn


(Larryeitel) #16

I know the pain!!!

Recently got a scaled-down version of Laradock working WITH full debugging!!! Checkout phpdock. The README outlines specific settings.

I decided to repo my docker-compose for two reasons. To share what it took for ME to get PHPStorm to play nice with Docker and to invite and refinements. It is called phpdock because at this point, I need to accommodate a variable number of php projects not necessarily Laravel all connecting to common services.


(Toozick) #17

I am having some problems and you seem to be able to get the remote_connect_back option to work over the remote_host one. Can you help? https://gist.github.com/Toosick/4639f4e33ad52c692876a89df89e2b40


(Visual4) #18

You don’t need 9000:9000 in docker, as it’s the container that will try to access your host - phpstorm acts as the server. Here your firewall might of course drop in, as some external process (the docker container) tries to contact your machine on port 9000.
You only need the following to get XDebug running:

RUN echo “zend_extension = xdebug.so” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “xdebug.remote_enable = 1” >> /etc/php5/fpm/conf.d/40-custom.ini
RUN echo “xdebug.remote_connect_back = 1” >> /etc/php5/fpm/conf.d/40-custom.ini

an of course you need to start XDebug by adding the XDebug Session Cookie manually, or with the Xdebug Helper Chrome extension.

Cheers,
Björn


(Toozick) #19

I have fixed my problem since but it’s not ideal like people have said. I am setting remote connect back to 0 and remote host to my host local ip. If I do the same and instead use the 172.18.0.1 gateway IP that the containers have it doesn’t work. That would be ideal as that is what the containers get when you use remote connect back. You think a firewall might be stopping the connection on IP 172.18.0.1?


(Otravers) #20

you need to find out the windows IP on the docker-windows internal net

Can you please explain how to do that?


(Visual4) #21

if you look at your logfile (Webserver log) you will see the IP, your requests come from. This should be the right one.

Cheers,
Björn