Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xdebug is not working with Docker for Mac #389

Closed
achekulaev opened this issue Nov 15, 2017 · 13 comments
Closed

Xdebug is not working with Docker for Mac #389

achekulaev opened this issue Nov 15, 2017 · 13 comments
Assignees
Milestone

Comments

@achekulaev
Copy link
Member

Xdebug does not seem to be working with Docker for Mac.

@ApacheEx
Copy link
Contributor

ApacheEx commented Nov 16, 2017

yeah, I had the same problem, as workaround I did:

  1. add into .docksal/etc/php/php.ini:
[xdebug]
xdebug.remote_connect_back = 0
xdebug.idekey = "PHPSTORM"
xdebug.remote_host = 10.254.254.254
  1. create a new network alias on your host machine:
    sudo ifconfig lo0 alias 10.254.254.254

some useful links:

@lmakarov
Copy link
Member

Try it with xdebug.remote_host = 192.168.65.1.

192.168.65.0/24 is the network used by Docker for Mac by default. 192.168.65.1 can be used to access the host from within the Moby VM and containers.

Docksal is using 192.168.64.0/24: 192.168.64.1 for host and 192.168.64.100 for the VM. On Linux the entire subnet is assigned as an alias on lo0.

You could technically change the default network Docker for Mac is using to 192.168.64.0/24, but that will result in conflicts if you decide to switch back to Docker + VirtualBox or use both at the same time.

image

@lmakarov
Copy link
Member

TL;DR

Short term, this can be used with Docker for Mac.

Add to .docksal/etc/php/php.ini and do fin reset cli:

[xdebug]
xdebug.remote_connect_back = 0
xdebug.remote_host = 192.168.65.1

In Docker for Mac, 192.168.65.1 is the host IP, which can be used by the Moby VM and containers to reach the host.


The long story

xdebug.remote_connect_back is the important part. It's set to 1 in our cli images by default, which tells xdebug to connect back for debugging to whomever initiated the original connection to the server. In this case, xdebug reads the remote server address from either $_SERVER['HTTP_X_FORWARDED_FOR'] or $_SERVER['REMOTE_ADDR'] variables.

On Docker for Mac, docksal-vhost-proxy sees host requests originating from the Docker bridge network interface, thus:

$_SERVER['HTTP_X_FORWARDED_FOR'] | 172.17.0.1

However, that bridge IP cannot be used to access the host back and the following will show up in xdebug logs (xdebug.remote_log to enabled logging):

$ fin exec tail -f /tmp/xdebug.log
Log opened at 2017-11-18 01:59:37
I: Checking remote connect back address.
I: Checking header 'HTTP_X_FORWARDED_FOR'.
I: Remote address found, connecting to 172.17.0.1:9000.
W: Creating socket for '172.17.0.1:9000', poll success, but error: Operation now in progress (29).
E: Could not connect to client. :-(
Log closed at 2017-11-18 01:59:37

With VirtualBox, host requests originate via the host-only network and docksal-vhost-proxy sees the correct host IP:

$_SERVER['HTTP_X_FORWARDED_FOR'] | 192.168.64.1

That one CAN be used by containers to reach back to the host.

With Docker for Mac/Windows, we've been setting the canonical Docksal IP (192.168.64.100), but have not been doing the same for the canonical host IP (192.168.64.1). They would both point to the host in this case (same as on Linux), but have to be distinguished to maintain the compatibility with the VirtualBox mode.

@lmakarov lmakarov self-assigned this Nov 18, 2017
@lmakarov lmakarov added this to the 1.6 milestone Nov 18, 2017
@achekulaev
Copy link
Member Author

achekulaev commented Nov 20, 2017

Needs to be added to docs/tools/xdebug.md

@lmakarov
Copy link
Member

Yes, I'm updating the xdebug docs as well. Not finished yet.

@wireblue
Copy link

In case this helps anyone else, Docker 18.03 (and later) enable you to use a special hostname to connect to the host.

xdebug.remote_host = "host.docker.internal"

This means you don't need to get/use the host's IP address anymore.

NOTE: This is for development purposes only and will not work in a production environment outside of Docker for Mac.

@achekulaev
Copy link
Member Author

@lmakarov should we update stacks with the above?

@ssuhss
Copy link

ssuhss commented Mar 1, 2019

@wireblue Tnks, you save my life with this line!

@achekulaev
Copy link
Member Author

@lmakarov looks like it is still the issue

still the issue

@achekulaev achekulaev reopened this Jul 2, 2019
@achekulaev achekulaev added this to To do in Docksal 1.13.0 via automation Jul 2, 2019
@JDDoesDev
Copy link
Member

I'll update the xdebug docs if you would like?

@achekulaev
Copy link
Member Author

Its not about the docs per se. If xdebug.remote_host = "host.docker.internal" works everywhere we should just update default stack files to use that and forget about the problem.

But this needs to be tested across different OSs and different Docker setups (Vbox/Docker Desktop)

@lmakarov
Copy link
Member

lmakarov commented Jul 3, 2019

host.docker.internal works on Windows and Mac, but not on Linux.

Linux support for this is almost there: docker/for-linux#264

@lmakarov
Copy link
Member

lmakarov commented Jul 3, 2019

I've just double checked this. We do not need to rely on host.docker.internal at all, since we have our own canonical and persistent host IP address that works in all environments: 192.168.64.1.

The default Docksal stack(s) ship with the necessary xdebug settings:

docksal/stacks/services.yml

Lines 146 to 149 in c0f2a12

- XDEBUG_ENABLED=${XDEBUG_ENABLED:-0}
- XDEBUG_CONFIG=remote_connect_back=0 remote_host=${DOCKSAL_HOST_IP} # Point xdebug to the host IP
- PHP_IDE_CONFIG=serverName=${VIRTUAL_HOST}
- DRUSH_ALLOW_XDEBUG=${XDEBUG_ENABLED:-0}

I checked this with Docker Desktop for Mac and xdebug works fine for me using the docksal/boilerplate-drupal8 repo (fin project create => Drupal 8).

If xdebug does not work in a project, then the project stack us likely custom and outdated.
See if you can get it to work in a boilerplate setup (fin project create). If not, then file a new issue providing all necessary diagnostics information.

@lmakarov lmakarov closed this as completed Jul 3, 2019
Docksal 1.13.0 automation moved this from To do to Done Jul 3, 2019
@lmakarov lmakarov removed this from Done in Docksal 1.13.0 Jul 3, 2019
@docksal docksal locked as resolved and limited conversation to collaborators Jul 3, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants