Phpstorm And Docker



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. For docker4drupal this means uncommenting these lines in the docker-compose.yml file:

If you enable these after you've created the PHP container, remember to restart the container so that xdebug is enabled and configured.

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. If the plugin is disabled, enable it on the Settings/Preferences Plugins page as described in Managing plugins.

Phpstorm And Dockery

Now go to Settings > Languages & Frameworks > PHP and set your language level as required. 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. Save this and apply.

  • PhpStorm can be setup to use Docker. Thanks to Gary Hockin’s excellent YouTube video Running PHPUnit Tests in PhpStorm with Docker, the setup process can be easily replicated. There is a four stage process: Configure PhpStorm to use Docker.
  • Preferences Languages & Frameworks PHP Test Framework (create new configuration to allow PHPSTORM find PHPUnit).

Next click the 'Add configuration' button on the taskbar in the top right. Pick the PHP Remote Debug template and click the + on the top left to add a new configuration (don't make the mistake of editing the template). Check 'Filter debug connection by IDE key' and set the IDE key to match the one in your XDebug config (for the above we'd use PHPSTORM). Add a server (the name doesn't matter). Give it the appropriate host and port—for docker4drupal the host will be something like http://myproject.docker.drupal.localhost, and the port will be 8000 unless you've changed it. Check 'Use path mappings', and set a path mapping for the root directory to /var/www/html (or wherever it's mounted to on the container).

It's important to note that for this to work with Drush, or anything that exists both in Composer and globally, you need to invoke Drush from the binary in the vendor directory vendor/bin/drush rather than using the system Drush, so that PHPStorm can use the path mappings. You could do this with a make command or Composer script. A make command I've used in the past:

Phpstorm Docker Xdebug Not Working

This is a modified version of the default docker4drupal command.

ddev

The process for ddev is largely the same. You can skip the step for setting up the CLI interpreter as this will work fine without it. An important point is that the name of your debug configuration must match your ddev site domain for this to work. See the ddev documentation for more details.

XDebug 3 only started working out of the box with PhpStorm version 2020.3

Phpstorm

Reference article - Configure Xdebug

The list of changes from XDebug 2 to 3 are available here for reference - Upgrade Guide

Phpstorm Docker Mysql

Configuring PhpStorm

Go to PhpStorm -> Settings -> Languages & Frameworks -> PHP -> Servers

  1. Click '+'
  2. Name docker-cli (Same as serverName under PHP_IDE_CONFIG environment variable)
  3. Host _
  4. Default 80
  5. Debugger Xdebug
  6. Check the checkbox next to 'Use path mappings'
  7. Modify the absolute path on the server to /var/www/html
Phpstorm and docker tutorial

Running the CLI Command

  1. Add breakpoints file
  2. In PhpStorm click the icon to 'Start Listening for PHP Debug connections'
  3. Run in the docker file - docker-compose run php-container php test.php
Phpstorm And Docker

Troubleshooting

  1. Check firewall or selinux if on linux
  2. The configuration host.docker.internal only became available under Mac and Windows with Docker version 20.04