9

I've just setup Docker on my machine & have an Nginx/PHP7 (FPM)/MySQL setup all working fine, but having installed Xdebug on the PHP container I can't get it to connect back to PHPStorm on my host machine.

Here's my PHP Xdebug config…

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-
20151012/xdebug.so
xdebug.remote_log=/usr/local/var/log/xdebug.log
xdebug.remote_enable=1
xdebug.remote_host=192.168.99.1
xdebug.remote_port=9000
xdebug.remote_connect_back=1
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true

When browsing, with the Xdebug enable cookie set for the container, there's no prompt for a connection. If I browse a locally hosted site, there is, so I know PHPStorm's listening correctly.

On the local machine, I can telnet to port 9000…

$ telnet 192.168.99.1 9000
Trying 192.168.99.1...
Connected to 192.168.99.1.
Escape character is '^]'.
^]
telnet> quit
Connection closed.

… however I cannot from either the boot2docker VM, or the container. When I try it just sits there doing nothing. Both the VM and the container can, however, ping the host machine just fine.

I've tried disabling my Mac's firewall, but still no joy.

I'm not quite sure how to disable the firewall on the boot2docker VM.

Any insight into why this won't work would be greatly welcomed. Thanks in advance.

TobyG
  • 1,692
  • 3
  • 21
  • 36

5 Answers5

10

Xdebug recommended config inside Container:

zend_extension = xdebug.so
xdebug.remote_enable = 1
xdebug.remote_connect_back = 0
xdebug.remote_host = docker.for.mac.localhost
xdebug.remote_port = 9000
xdebug.remote_handler = dbgp
xdebug.remote_mode = req
xdebug.remote_autostart = 1
xdebug.idekey = PHPSTORM

Since Docker-17.06, you can access services hosted on Mac inside Container, via the static host name: docker.for.mac.localhost

I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST ?
The Mac has a changing IP address (or none if you have no network access). From 17.06 onwards our recommendation is to connect to the special Mac-only DNS name docker.for.mac.localhost which resolves to the internal IP address used by the host.

see https://docs.docker.com/docker-for-mac/networking/#i-cannot-ping-my-containers

Lyfing
  • 1,778
  • 1
  • 19
  • 20
  • 1
    Just to add to this, if anyone is trying to achieve this on a remote host, you need to take the same approach, set the `remote_host` to the host server IP and then create a SSH tunnel from your local machine to the remote host. SSH tunnel will by default bind to the loopback interface though (which your container can't access), so you need to update your `sshd_config` file and set `GatewayPorts yes`, create your SSH tunnel and you will bind to `0.0.0.0:{port}` instead.. xdebug should then be able to make a connection to your IDE – doz87 Nov 10 '18 at 04:12
  • 4
    `xdebug.remote_host = host.docker.internal` is working since Docker version 18.03.0-ce-mac59, 2018-03-26 – eric.chenchao Apr 10 '19 at 04:03
4

I solved this by changing the client_port to something other than 9000 because my Mac had php-fpm listening on port 9000 already.

I used this to see what ports were being listed to and by what:

sudo lsof -nP -iTCP -sTCP:LISTEN

This is all I need for Xdebug in my php.ini inside the container:

zend_extension=xdebug.so
xdebug.mode=debug
xdebug.client_host=host.docker.internal
xdebug.client_port=9009

Then set the Debug port in PHPStorm in the Xdebug section of the Debug page in the PHP settings also to 9009

Kevin
  • 121
  • 1
  • 4
1

After spend some time trying to solve it, I found a solution. It works for me. You have to add the pathMappings on the launch.json file. Like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "/var/www/html/": "${workspaceFolder}"
            }
        },
    ]
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

You need to use a network that is bridged from your Docker host to your Mac. Do ifconfig on the Mac and look for the local IP on other local networks, e.g., 10.0.1.13. (The specifics may differ by version of Docker, but this worked with a Vagrant as Docker host and should work for most VMs.)

ldg
  • 9,112
  • 2
  • 29
  • 44
  • Thanks for the tip. I've added a bridged n/w adapter on my wireless adaptor, and I can see a new adapter in the docker VM, with the IP address 192.168.1.18 (host is 192.168.1.6), but I still can't telnet from the VM or the container. Can you offer more details on how to set this up correctly? – TobyG Jul 05 '16 at 05:43
  • You _shouldn't_ need another bridge. Did you try a different port, btw? FPM may be using 9000 so it could be conflicting. Try using 10000 or similar. You can also do `nc -l 8888` on your Mac and telnet from the container to verify it works independently of xdebug. – ldg Jul 05 '16 at 06:30
  • Turns out rebuilding the VM worked. I'd tried everything else. In the end I removed the VM, removed all the host only networks, and recreated the VM. If all else fails… reboot! – TobyG Jul 05 '16 at 10:44
0

this worked for me:
xdebug.idekey="VSCODE"
xdebug.default_enable=1
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.profiler_enable=0
xdebug.profiler_output_dir="/var/www/html"
xdebug.remote_connect_back=0
enter code here xdebug.cli_color=1
xdebug.var_display_max_depth=10
xdebug.remote_host= "host.docker.internal"

Also i had to remove these xdebug related environment variables from docker-compose :

XDEBUG_CONFIG: "remote_host=localhost"
PHP_IDE_CONFIG: "serverName=Docker"
from my docker-compose. here is the old setting for docker compose:

enter image description here

please let me know if any questions :)

Guru
  • 922
  • 9
  • 12