Skip to content

Instantly share code, notes, and snippets.

@antfroger
Last active November 9, 2024 20:13
Show Gist options
  • Save antfroger/1f2b24fdba0f215a41c8a94e8aa062f7 to your computer and use it in GitHub Desktop.
Save antfroger/1f2b24fdba0f215a41c8a94e8aa062f7 to your computer and use it in GitHub Desktop.
Using xdebug with Windows 10, WSL2, Docker and VS Code

Configuring xdebug to work with Windows 10 (WSL2), Docker and VS Code

Configuring your dev environment to be able to use xdebug when you're working on Windows 10 (with WSL2) and Docker with VS Code can be (a bit) tricky.
This is a quick reminder of how I've done that.

Configuring the environment

  1. Install and configure xdebug in Docker

    # Install xdebug according to the Docker image you're using
    RUN pecl install xdebug
    COPY xdebug.ini  $PHP_INI_DIR/conf.d/

    (cf. the xdebug.ini file in this gist)

  2. Setup VS Code

    PHP Debug is the extension to debug PHP in VS Code.
    Once installed and your project opened, create the configuration file .vscode/lauch.json
    (cf. the lauch.json file in this gist)

    Two impostant things here :

    • The port must match the one defined in xdebug.ini
    • pathMappings must associate the path of your app on Docker and the path of your VSCode project
      (do not use an absolute or relative path. You must use the variables VSCode provides)
  3. Configure Windows Defender Firewall

    Even after starting debugging, breakpoints might never be hitten. That's because of Windows Defender Firewall.
    It treats WSL as a public network by default and blocks access. This can be fixed by adding a rule. Just run (with Powershell):

    New-NetFirewallRule -DisplayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow

    More about this issue here

Debug a request

Now that your environment is set up, it's time to debug!

  1. Run a debugging session

    In the left side panel of VSCode, select the "Debug view" (or just press Ctrl + Shift + D), make sure that the chosen configuration is the one you added in the file lauch.json. Then click on the little green triangle titled "Start debugging".

  2. Add breakpoints wherever you want to debug your app

  3. Refresh your browser!

Optional:
You can add Xdebug helper to debug in Chrome.

Yeah!
You're ready to create an awesone app!

Want to know more on debugging with xdebug?
Let's read the 'Step Debugging' doc

{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
// I changed the port, by default xdebug 3 uses 9003
"port": 5902,
"pathMappings": {
"/var/www/my-app": "${workspaceFolder}"
},
"hostname": "localhost"
},
]
}
zend_extension=xdebug
xdebug.mode=debug,develop
xdebug.client_host=host.docker.internal
xdebug.client_port=5902 #same port as in launch.json
#Uncomment this line to enable xdebug for each request
#xdebug.start_with_request=yes
@androsland
Copy link

androsland commented Sep 12, 2024

I'll leave this here for anyone that uses docker-compose.yml and can't get this to work.

So, for me, the Configure Windows Defender Firewall step wasn't necessary.

I only needed to add host.docker.internal:host-gateway as an extra_hosts to my service (docker-compose.yml) like this:

services:
  wordpress:
    build:
      context: .
      dockerfile: Dockerfile-wordpress # this is the filename of your Dockerfile
    extra_hosts:
      - "host.docker.internal:host-gateway"

and everything worked!
I found this here: https://blog.bitexpert.de/blog/win10_wsl2_docker_xdebug

Note: In my Dockerfile, I installed xdebug like this:

RUN pecl install xdebug \
    && docker-php-ext-enable xdebug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment