I have a PHP project based on Drupal and I use docker-compose to test it locally. In docker-compose.yaml I have a bind mount: volumes: -./modules/custom/test. Setting up xdebug with Docker and PHPStorm. A quick guide to how to do this, since I always forget and have to look it up. This is done with docker4drupal, but should work equally well with any Docker Compose setup. Enable xdebug on the relevant Docker container.
Run Docker-Compose via PHPSTORM. Click Docker Button in PHPSTORM toolbar; Connect to Docker; Run all docker container; Twig configuration Allow autocomplete for objects Preferences Languages & Frameworks PHP Symfony Twig / Template: Namespace: main; Path: src/Resources/views. Docker enables developers to deploy applications inside containers for testing code in an environment identical to production. PhpStorm provides Docker support using the Docker plugin. The plugin is bundled and enabled by default. The Docker plugin is bundled with PhpStorm and activated by default. It's the next video in a series about the usage of PHPUnit and various interpreters in PhpStorm. Here Gary Hockin, PhpStorm Developer Advocate, explains how.
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:
- Enable the xdebug extension.
- Enable automatic start on every request (see note on this below).
- Increase default maximal function nesting level, because it is often not enough.
- 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
PHP_IDE_CONFIG
env variable toserverName=localhost
. This will tell your PhpStorm which server configuration to use. See next step for details.
Docker Compose Webstorm
3. Configure server in PhpStorm
Docker Compose Intellij
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.