Skip to content
Advertisement

Docker Cannot assign requested address

I think I’m pretty close (I hope) to having xdebug running in a docker container, aiming to connect via Visual Studio Code.

I think that maybe I’m supposed to add a config to the /etc/hosts file in the container, directing an IP address to the url my files are being served over, but am not sure what that IP address is supposed to correspond to.

The error:

via xdebug_info() (and in /tmp/xdebug.log):

JavaScript

via telnet localhost 9003:

JavaScript

The xdebug.ini file:

JavaScript

The launch.json:

JavaScript

The Dockerfile:

JavaScript

Finally the docker-compose.yml:

JavaScript

Result of xdebug_info() shows:

JavaScript

I bet I need to add a configuration, but not sure what.

Update

From this tutorial, I see that xdebug.discover_client_host=true “tells Xdebug to attempt to extract the IP of the client from the HTTP request”, falling back to client_host if that fails. (It checks $_SERVER['HTTP_X_FORWARDED_FOR'] and $_SERVER['REMOTE_ADDR'] variables to find out which IP address to use.) This is the config that replaces xDebug 2’s remote_connect_back. Most of xDebug’s significant configs have changed between v2 and v3.

Still getting the same connection errors, though:

JavaScript

And in the logs:

JavaScript

I tried adding an /etc/hosts entry pointing 192.168.16.1 to myproject.test (in the Docker container) but haven’t figured out how to flush the DNS records in the container. Maybe I need to add extra host mapping in the container?

Came across a post that suggested Lando, which appears to wrap Docker, can ease some of this pain, but I’m not ready to give up on this yet, so thank you very much if you’ve read this far.

Solution (gratefully) accepted below.

I also needed to make a change to the VS Code launch.json file:

JavaScript

Because I don’t have the WP codebase in my repo, just a specific plugin. With "stopOnEntry": true, the xDebug plugin was looking for an (non-existent) index.php file (starting point) in the "${workspaceRoot}, as opposed to just checking for breakpoints, which is what I required, my local codebase just being part of the entire application.

Also, I think that the # comments in the accepted answer were invalidating the .ini file, so watch out for that if it seems like your settings aren’t coming up.

Advertisement

Answer

It looks like you are using Xdebug 3 while some of your configuration parameters are from deprecated Xdebug 2 (remote_host and remote_connect_back which is conflicting with new discover_client_host) – see https://xdebug.org/docs/all_settings. Let’s start with cleaning up those:

JavaScript

These settings should force Xdebug to always connect to your Docker host IP. Depending on your Docker version and environment, you may need to add following setting to your docker-compose.yml:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement