Using Xdebug with Docker Compose and PhpStorm

1. Add Xdebug to your PHP application container

Add following lines to your php Dockerfile:

1
2
3
4
# Install Xdebug
RUN yes | pecl install xdebug \
&& echo "zend_extension=$(find $(php-config --extension-dir) -name xdebug.so)" \
> /usr/local/etc/php/conf.d/xdebug.ini

2. Add necessary environment variables

docker-compose.override.yaml

1
2
3
4
5
6
7
8
9
services:
your_php_app_service:
environment:
XDEBUG_ENABLED: 1
XDEBUG_REMOTE_AUTOSTART: 1
XDEBUG_MAXNESTING_LEVEL: 1000
XDEBUG_REMOTE_CONNECT_BACK: 1
XDEBUG_REMOTE_HOST: host.docker.internal
PHP_IDE_CONFIG: serverName=localhost

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

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:

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