devcontainers / templates

Repository for Dev Container Templates that are managed by Dev Container spec maintainers. See https://github.com/devcontainers/template-starter to create your own!
https://containers.dev/templates
MIT License
859 stars 226 forks source link

How to initiate xdebug via web browser? #219

Closed bluesherpa closed 7 months ago

bluesherpa commented 7 months ago

Hello, Visual Studio Code is running a Docker container built using the PHP MariaDB container. I found that I can debug a PHP script when I start the debug session from within Visual Code. Since the container can be configured to run Apache and serve /var/www/html from the root directory of my Visual Code project, I can effectively serve web pages contained in the container and access them from my local web browser. This made me wonder - is it possible to initiate an xdebug session from my local browser so that I can use Visual Code to step through the code as it executes? Thanks! -Ed

bluesherpa commented 7 months ago

Figured it out in case anyone else runs into this question...

Here is what I did to enable debugging of PHP scripts running in a Docker dev container when the scripts are called from my local browser...

From Visual Studio Code: F1 > Dev Containers: New Dev Container > PHP & MariaDB

Once the container is built, go into .devcontainer\devcontainer.json:

  1. add port 9000 to the forwardPorts
  2. uncomment the "postCreateCommand", you'll have to add a comma in front of the line.

Add .vscode\launch.json - see attached copy. I had to modify the default file created by Visual Studio Code - the first configuration for "Listen for Xdebug" defaulted to port 9003, but it's supposed to be port 9000.

F1 > Rebuild container

Open a Terminal window and run "apache2ctl start"

I created index.php and put a couple of lines of PHP code and then added a debug breakpoint to a line at the beginning.

At the bottom of Visual Studio Code, I clicked "Listen for Xdebug". That put VS Code into debug mode.

Open a local browser window to http://localhost/?XDEBUG_SESSION_START=myDebug

The browser started to load the page and VS Code started stepping through the code :)

Let me know if you have any questions about this...

samruddhikhandale commented 7 months ago

This is wonderful, thank you so much for taking the time in sharing this valuable information. Appreciate it ✨

Closing this issue as it's resolved.