Docker Xdebug



1. Add Xdebug to your PHP application container

Add following lines to your php Dockerfile:

2. Add necessary environment variables

Php docker xdebug phpstorm

Docker Php Fpm Xdebug

  • The docker run command accepts command line options to specify volume mounts, environment variables, the working directory, and more. Getting a shell for build/tooling operations Getting a shell into a build container to execute any operations is the simplest approach.
  • Next to 'PHP Interpreter' to bring up the interpreters. Choose 'Docker (SSH)' in the left pane and click on the little folder icon on the bottom of the window next to 'Configuration options'. In the pop up enter xdebug.remotehost as key and host.docker.internal as value and hit 'OK'.

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.

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.

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

Docker Xdebug 3

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.

Docker xdebug remote host

Debugging

Phpstorm xdebug docker

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.

Docker Xdebug Vscode

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.

Phpstorm Xdebug Docker Cli

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

Wsl2 Xdebug

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

Vscode Php Debug Docker

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