0

Let me start by saying I know this question has been asked, but I've been through all the answers I could find and none so far have worked for me. I'm also a frontend dev and my networking experience is pretty limited, so forgive my ignorance.

I'm running Docker Desktop on Windows 11 using WSL2 integration. I'm running a local WordPress install through Docker-Compose and can't for the life of me get Xdebug to connect.

Here's my docker-compose.yml:

version: "3.1"

services:
  wordpress:
    build: ./
    ports:
      - "80:80"
    volumes:
      - ./wp-content:/var/www/html/wp-content:delegated
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
    depends_on:
      - db
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: wordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
    volumes:
      - db-data:/var/lib/mysql:delegated

volumes:
  db-data:

My Dockerfile:

FROM wordpress:latest

# We're going to use this path multile times. So save it in a variable.
ARG XDEBUG_INI="/usr/local/etc/php/conf.d/xdebug.ini"

# Install AND configure Xdebug
RUN pecl install xdebug \
    && docker-php-ext-enable xdebug \
    && echo "[xdebug]" > $XDEBUG_INI \
    && echo "xdebug.mode = debug" >> $XDEBUG_INI \
    && echo "xdebug.start_with_request = yes" >> $XDEBUG_INI \
    && echo "xdebug.discover_client_host = 0" >> $XDEBUG_INI \
    && echo "xdebug.client_port = 9003" >> $XDEBUG_INI \
    && echo "xdebug.client_host = host.docker.internal" >> $XDEBUG_INI \
    && echo "xdebug.log = /tmp/xdebug.log" >> $XDEBUG_INI

And my launch.json:

{
    // 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": 9003,
            "stopOnEntry": true, 
            "pathMappings": {
                "/var/www/html": "${workspaceRoot}"
            },
        },
    ]
}

When I call xdebug_info(), I get the following error that others have mentioned before:

[Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port).

Interestingly, if I ifconfig from inside a WSL terminal and replace xdebug.client_host = host.docker.internal with my specified IP, the error message changes to:

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

Any help with this would be much appreciated. I feel like I'm so close and am just missing some piece of connectivity. Thanks so much in advance.

kdsprogrammer
  • 136
  • 1
  • 13
  • 1) Is VSCode actually listens on Xdebug port at that moment. Double check with `netstat` or any similar tool if there are listeners on TCP 9003 port. 2) Unlikely but worth checking: could be IPv4 vs IPv6 issue... 3) Check Windows Firewall as well (or your Internet Security app if you have one) to ensure that you can make such connections from WSL subsystem/interface. 4) You can also try this: start listening for Xdebug in VSCode, login into WSL shell and using `telnet` or similar app try to connect back to host on Xdebug port -- are you able to make a connection? What about another port – LazyOne Jan 22 '23 at 14:02
  • @LazyOne 1) Yes, the listener is working. 2) How would I go about looking at this? 3) I added allowance rules, but either way I have temporarily disabled firewall. 4) That one is interesting - If I try to telnet host.docker.internal 9003 from inside a wsl2 term, I get "unable to connect to remote host: connection refused". This is with the firewall off. Any idea why that might be happening? Thanks for your help. – kdsprogrammer Jan 22 '23 at 16:49
  • 1) What app is listening? Must be VSCode. 2) I do not use WSL myself, but have seen a few questions here on Ubuntu mainly where Docker or proper VM had issues connecting to a host OS because of that. I guess on Windows you need to see what IPs are used to listen on Xdebug port. IPv6 should have different presentation as compared to IPv4 (see outputs in https://stackoverflow.com/questions/72317535/xdebug-3-0-wsl2-and-vscode-address-is-already-in-use-by-docker-proxy as an example). – LazyOne Jan 22 '23 at 19:41
  • 4) *"If I try to telnet host.docker.internal 9003 from inside a wsl2 term"* I believe `host.docker.internal` does not really work when using Docker on WSL, you better actual host IP directly. Try with that `wsl2_ip_here` that you are getting. It could be Firewall (WSL or Windows one). This is just basing on similar questions here, not an actual personal experience. – LazyOne Jan 22 '23 at 19:43
  • @LazyOne yeah even if I telnet wsl2_ip_here I still get a connection refused error. and this is with Windows defender completely turned off. This may just be too over my head at the moment, I'm gonna look at other ways I can debug. – kdsprogrammer Jan 22 '23 at 21:22

0 Answers0