devilbox: Xdebug can't connect on Docker for Mac

If you encounter a bug and something does not work, make sure you have done the following and check those boxes before submitting an issue - thank you!

  • Pull latest dockers (e.g.: docker pull cytopia/<used_docker>) before running docker-compose up
  • Specify used docker versions (php, web and database)
  • Attach logs for php, mysql and webserver (found in log/ directory)
  • Start with debug mode and attach docker-compose output (.env setting DEBUG_COMPOSE_ENTRYPOINT=1)
  • Never use different mysql|mariadb versions on the same HOST_PATH_MYSQL_DATADIR on existing database files. Different mysql|mariadb versions might upgrade/corrupt existing database files. If you have done that already, start with a different path of HOST_PATH_MYSQL_DATADIR (to an empty directory) and try again.

Please also specify the following info:

  • Which operating system are you at (Linux, OSX or Windows)

macOS Sierra 0.12.6

  • docker version
Docker version 18.03.1-ce, build 9ee9f40
  • docker-compose version
docker-compose version 1.21.0, build 5920eb0
docker-py version: 3.2.1
CPython version: 3.6.4
OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018

xdebug.log

xdebug.remote_enable=on
xdebug.remote_host=host.docker.internal
xdebug.remote_connect_back=0
xdebug.idekey=code
xdebug.remote_log=/var/log/php/xdebug.log

I’m using vscode with the following configuration

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "stopOnEntry": true,
            "log": true,
            "pathMappings": {
                "/shared/httpd/test/htdocs": "${workspaceFolder}"
            }
        }
    ]
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 33 (15 by maintainers)

Commits related to this issue

Most upvoted comments

@pvantilb for OSX you should create a network alias on localhost interface and use this for the remote_host:

  1. How to create network alias: https://github.com/devilbox/xdebug
  2. xdebug.remote_host = 10.254.254.254

Documentation will be ready shortly

@Tuurlijk Thanks so much! i guess i jumped the gun assuming that the curl meant it wasn’t working. Once i adjusted xdebug.ini with the alias, phpstorm was able to receive xdebug connections and step throught breakpoints. Thanks for the detailed steps, everything is working now!

@pvantilb I tried on a mac yesterday and it would not work without the network alias on the host machine. Once I enabled the alias, it all worked.

Bear with me, there are many steps you need to get right. It’s easy to fail setting up xdebug 😉

Please report back which steps already work for you and we’ll go from there.

You get the devilbox status page when you call 10.254.254.254 because you have aliased that IP to 127.0.0.1 on the host. So that works as expected!

Calling curl on 10.254.254.254 from the php container also gives you the devilbox page because it’s linked to the localhost loopback interface. So that also works as expected.

This means you’ve successfully set up the loopback alias. Good job! 👍

The key here is understanding the xdebug config settings. I normally have a working setup when using remote connect back. Xdebug will figure out the remote host to connect back to from the http request sent by the browser on the host machine. In the case of Docker on Mac, this does not seem to work. The reason for this is that docker on Mac still is not entirely native, but running through an very thin virtual machine. I think.

Xdebug will try to connect to port 9000 on 10.254.254.254. So you can try to curl that port when trying to verify that phpstorm is listening there. Don’t know if that will work. You’ll need to learn how to speak the dbg protocoll, which is xml messages I believe.

One of the best articles on setting up xdebug at the moment: https://enrise.com/2018/02/debugging-php-with-xdebug/

Step 1

xdebug.remote_connect_back = Off
xdebug.remote_enable = On
xdebug.remote_port = 9000
xdebug.remote_host = 10.254.254.254
xdebug.remote_log=/var/log/php/xdebug.log
  • Inspect the php configuration in the devilbox panel. There is a phpinfo page in the info menu. Verify that your php settings are indeed set

If you have done this, you should be all set. Once you’ve restarted the php container, you can inspect the logfile:

# From the devilbox root folder:
./shell.sh

# From inside the php container:
tail -f /var/log/php/xdebug.log

Now you can see exactly what is going on.

Step 2

Create a test site and verify that it works. Note that te site will run on port 80 by default on the host machine. You don’t need any special ports like 8080 unless you have adjusted the default configuration files for devilbox. You should be all set when going to http://127.0.0.1 gives you the devilbox page.

In your case, you report working devilbox sites on port 8080, great. You test site should be available on port 8080 too.

  • Create a very simple site in your data dir: ./data/www/test/htdocs/index.php.
  • Put some very simple content in there on which you can set a breakpoint:
<?php
$blah = 'Ohai';
$lala = $blah;
echo $blah;
phpinfo();
  • verify that you can see the test site in your browser by calling: http://test.loc or http://test.loc:8080 in your case. If you can’t see the site, create an entry in your local hosts file: 127.0.0.1 test.loc.

Part 3

Configure the browser and your IDE.

  • Set a breakpoint in the index.php
  • Setup a debug server
  • Setup the proper path mappings for your site
  • Verify the xdebug test script works
  • Enable xdebug listening mode in phpstorm
  • Enable debugging in the browser using a xdebug helper extension with a cookie name: PHPSTORM
  • Refresh the page in the browser

@longelas thanks for the solution. I will re-open and add this to the documentation.