'Xdebug can't connect back to Docker host

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.



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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}"
            }
        },
    ]
}

Solution 4:[4]

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.)

Solution 5:[5]

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 :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lyfing
Solution 2 Kevin
Solution 3 Tyler2P
Solution 4 ldg
Solution 5 Guru