'Xdebug:Time-out connecting to debugging client

I am trying to setup xdebug using laravel sail with latest version, on my Ubuntu 20.04.3 LTS desktop,

ie
php 8.1
laravel 9

with below mentioned details to .env file

SAIL_XDEBUG_MODE=develop,debug
SAIL_XDEBUG_CONFIG="client_host=172.23.0.1"

but when i run sail debug migrate, returns

Xdebug: [Step Debug] Time-out connecting to debugging client, waited: 200 ms. Tried: 172.23.0.1:9003 (through xdebug.client_host/xdebug.client_port)

When I tried finding IP, it change every time
docker inspect -f {{range.NetworkSettings.Networks}}{{.Gateway}}{{end}} <container-name> return different IP like 172.25.0.1



Solution 1:[1]

Solution

Just put this in your .env the following

SAIL_XDEBUG_MODE=develop,debug
SAIL_XDEBUG_CONFIG="client_host=host.docker.internal"

Just as in Windows and MacOS

Now you can run sail debug migrate or just sail debug in order to test it.

Explanation

You're probably using sail down to 'stop' the containers, but as the docker documentation stated:

docker-compose down

...

Stops containers and removes containers, networks, volumes, and images created by up.

By default, the only things removed are:

  • Containers for services defined in the Compose file
  • Networks defined in the networks section of the Compose file
  • The default network, if one is used

Thus, every time you're running sail up it will regenerate the network with a new IP address.

You could use sail stop, but when you're working with multiple projects it could become a hassle to edit the ports (in the .env file) of every service per project.

As per v1.10.1, sail incorporate the extra_host option, essentially detecting the IP of the host machine:

With this, every time we create/start the application container the host.docker.internal mapping will be created and will point to the host machine.

Edit: Do not forget to update Docker since this only works with Docker v20.10+.

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 Farid Nogales