Xdebug Docker Phpstorm

  



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. It is possible to run PHP cli without a docker-compose file, I have found it is easier to set up PhpStorm using this intermediate step. PhpStorm has several preconfigured Docker containers, source: Github - JetBrains / phpstorm-docker-images; Docker hub - PhpStorm; They can be used as follows: Php 7.3 CLI and XDebug 2.7. # Configure PhpStorm PHPStorm acts as a debugging client. It accepts connections from xDebug extension and allows you to set breaking point, evaluate variables and other good stuff. First of all, you need to configure the xDebug. Instruct XDebug to connect back to the IP where web request came from. Instruct XDebug to connect to host.docker.internal for command line execution or whenever “connect back” is not possible. Set PHPIDECONFIG env variable to serverName=localhost. This will tell your PhpStorm which server configuration to use. See next step for details.

Phpstorm Docker Xdebug Not Working

Docker for Windows requires a well known IP address in order to connect to the host operatingsystem.

Table of Contents

  • Prerequisites
  • Configuration

Ensure you know how to customize php.ini values for the Devilbox and have a rough understandingabout common Xdebug options.

On Windows you will have to manually retrieve the IP address to which Xdebug should connect tovia xdebug.remote_host.

Xdebug

When you have done no custom configuration in your Virtual Switch manager, Docker for Windows willuse the DefaultSwitch automatically.

Windows: Virtual Switch Manager example screenshot

Phpstorm
  1. Open command line

  2. Enter ipconfig

  3. Look for the IP4 address in DefaultSwitch (e.g.: 192.168.0.12)

Docker

Important

192.168.0.12 is meant as an example and will eventually differ on your system.Ensure you substitute it with the correct IP address.

For the sake of this example, we will assume the following settings and file system paths:

DirectoryPath
Devilbox git directory/home/cytopia/repo/devilbox
HOST_PATH_HTTPD_DATADIR./data/www
Resulting local project path/home/cytopia/repo/devilbox/data/www
Selected PHP version5.6
DockerNAT IP address192.168.0.12
Virtual Switch IP address192.168.0.12

The Resulting local project path is the path where all projects are stored locally on yourhost operating system. No matter what this path is, the equivalent remote path (inside the Dockercontainer) is always /shared/httpd.

Important

Remember this, when it comes to path mapping in your IDE/editor configuration.

1. Ensure Xdebug port is set to 9000

2. Set path mapping

Create a new PHP server and set a path mapping. This tutorial assumes your local Devilbox projectsto be in ./data/www of the Devilbox git directory:

Important

Recall the path settings from the Assumption section and adjust if your configuration differs!

3. Ensure DBGp proxy settings are configured

Note

The following example show how to configure PHP Xdebug for PHP 5.6:

Create an xdebug.ini file (must end by .ini):

Copy/paste all of the following lines into the above created xdebug.ini file:

Important

Ensure you have retrieved the correct DockerNAT IP address as stated in the prerequisites section above!

Note

Host os and editor specific settings are highlighted in yellow and are worth googling to get a better understanding of the tools you use and to be more efficient at troubleshooting.

Restarting the Devilbox is important in order for it to read the new PHP settings.Note that the following example only starts up PHP, HTTPD and Bind.

See also

Stop and Restart (Why do docker-composerm?)

1. Add Xdebug to your PHP application container

Add following lines to your php Dockerfile:

2. Add necessary environment variables

docker-compose.override.yaml

Here we do following things:

Xdebug
  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.

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.

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 Docker Phpstorm

Phpstorm docker xdebug path mappings

Xdebug Phpstorm Docker Not Working

  • 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.

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