skip to Main Content

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

2

Answers


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

    Login or Signup to reply.
  2. I’ve been searching on this till I read about it being a firewall port issue here.

    Then I remember the services.[laravel.test].ports on docker-compose.yml and tried to add 9003:9003 ports.

    Try adding the XDebug ports:

    services:
        laravel.test:
            build:
                context: ...
                dockerfile: Dockerfile
                args:
                    WWWGROUP: '${WWWGROUP}'
            image: ...
            extra_hosts:
                - 'host.docker.internal:host-gateway'
            ports:
                - '${APP_PORT:-80}:80'
                - '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
                - '9003:9003'
            ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search