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.
-
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)
-
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)
-
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
Now that your environment is set up, it's time to debug!
-
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". -
Add breakpoints wherever you want to debug your app
-
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
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 anextra_hosts
to my service (docker-compose.yml) like this: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