Docker Php Cli Xdebug



Php xdebug config

1. Add Xdebug to your PHP application container

Php Xdebug Config

Xdebug php docker
  • When I added to xdebug.ini: xdebug.clienthost = 172.17.0.1 and xdebug.startwithrequest = yes and ran export PHPIDECONFIG='serverName=localhost' but I don't use Windows/MacOs so I can use host.docker.internal like clienthost. The IP doesn't seem like good idea (docker and change this IP?). Next question - Why it works on web server in browser?? – viko Feb 22 at 21:55.
  • PHP 7.1 cli with xdebug and sonar scanner This image allows you to run and debug your PHP code and the ability to run sonar scanner on your project thanks to Docker.
Docker Php Cli XdebugDocker

Add following lines to your php Dockerfile:

2. Add necessary environment variables

docker-compose.override.yaml

Here we do following things:

  1. Enable the xdebug extension.
  2. Enable automatic start on every request (see note on this below).
  3. Increase default maximal function nesting level, because it is often not enough.
  4. Instruct XDebug to connect back to the IP where web request came from.
  5. Instruct XDebug to connect to host.docker.internal for command line execution or whenever “connect back” is not possible.
  6. Set PHP_IDE_CONFIG env variable to serverName=localhost. This will tell your PhpStorm which server configuration to use. See next step for details.

While designed for web development, the PHP scripting language also provides general-purpose use. All legacy individual and organizational repository plans expire on their January 2021 billing cycle date. Xdebug is an extension for debugging your PHP code. Magento Cloud Docker provides a separate container to handle Xdebug requests in the Docker environment. Use this container to enable Xdebug and debug PHP code in your Docker environment without affecting your. A brief guide on configuring xdebug with Docker. A brief guide on configuring xdebug with Docker. Add a new CLI interpreter by clicking. Click the + on the top left and add a new Docker configuration. Select Docker Compose and the php service, then use the default options for everything else.

3. Configure server in PhpStorm

In your PhpStorm Settings go to Languages and Frameworks > PHP > Servers and add a new server:

  • Name: localhost
  • Host/Port: whatever host and port you use to open your local website, for example: ‘magento.localhost’ and ‘8080’.
  • Debugger: Xdebug
  • Use path mappings: yes

Configure the path mapping according to your source code volume mount in docker-compose.yaml.
I have the following mount: ./magento:/var/www/html, therefore my local ./magento directory is mapped to the /var/www/html path on the server.

Debugging

One important thing you need to do is to start listening for PHP debug connections with a small phone icon in your PhpStorm.

Phpstorm docker xdebug not working

Autostart

Normally you would need a browser extension, which adds debug session start flag to your requests when you need it.
However I found it convenient to enable autostart and only control the XDebug via PhpStorm.
The case is that when XDebug tries to start the debugging session but the remote host is not listening, the XDebug does not continue.
So when I don’t need to debug, I just switch listening off. I believe this still adds a small overhead in time for all requests, but for me it is unnoticeable.

If you don’t like this approach, just disable the autostart and start the session your own way (see: Activate debugger).

Creating Run/Debug configurations in PhpStorm

Sometimes it is useful to create and store some specific configuration so you can run it over and over.
I will not describe the whole Run/Debug configurations topic here but only one Docker-specific aspect: you need to teach your PhpStorm to run PHP interpreter inside your container.

For this you need to create a new PHP CLI interpreter configuration:

Xdebug Php Docker

  • in your PhpStorm Settings go to Languages and Frameworks > PHP and click the ‘…’ button near the “CLI Interpreter” field.
  • in new window add a new interpreter “From Docker, Vagrant, VM, Remote…”
  • choose “Docker Compose” radiobutton,
  • select or create new Server (use Unix socket to connect to Docker daemon)
  • choose Docker Compose Configuration files; in my case I choose two in following order:
    • docker-compose.yaml
    • docker-compose.override.yaml
  • select your PHP app service
  • choose “Connect to existing container” instead of starting a new one.

Phpstorm Docker Xdebug Not Working

This should be enough, save everything and create your Run/Debug configuration using this CLI interpreter.





Comments are closed.