Docker Xdebug Phpstorm



I’ve be searching a days to a solution but not success. I installed Xdebug 2.5.5 in my Docker container and added it to my php.ini as follow. Xdebug zendextension = '/usr/local. 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). PHPSTORM + XDEBUG (2/3) + WSL2 + DOCKER. Small example project to show how to configure xdebug (2 or 3) with WSL2 and Docker. This solution works only if you use PhpStorm on WSL2 with X server! WSL2 change Linux IP every reboot time, so you should, add global variable (WSLIP) in your Linux system.

  1. Phpstorm Xdebug Docker Cli
  2. Docker Phpstorm Xdebug Nginx
  3. Phpstorm Docker Compose
July 26, 2018

To follow this tutorial, you must have the Xdebug extension installed on your container. In my example, I will use a Docker image of TheCodingMachine created by David Négrier. If you are starting a project, I recommend you to download one of our images here TheCodingMachine Docker PHP images

My base docker-compose.yml file looks like this:

Note: the PHP_EXTENSION_XDEBUG environment variable is specific to the thecodingmachine/php. It automatically configures Xdebug for the container.

Ports configuration

Keep in mind that PHPStorm opens port 9000 (by default) so Xdebug can connect to it. Our PHP container has Xdebug installed and Xdebug will try to connect to PHPStorm on port 9000 by default.

Out of the box, everything should be fine...

... except if port 9000 is already taken by another program on your host (your machine running PHPStorm).

Docker Xdebug Phpstorm

Switching port

Port 9000 can be already used. In particular, if you are using PHP-FPM, the default port for PHP-FPM is 9000.This directly conflicts with the XDebug port.

Phpstorm xdebug docker cli

If you want to use another port for Xdebug, follow the instructions below.

I recommend to use port 10000 instead.

Otherwise, just keep the default configuration port.

Registering your container server in PHPStorm

First, click on edit configurations, on the top right of PHPStorm window.

Then, follow the explanation in picture. All numbers are explained below.

(1) Click on add button

(2) Confirm it by clicking on PHP Web Page

(3) Define a name for your project

(4) Click on ... to add a debug server

(5) Once it's done, you are on debug server page. Click on '+' button to create a new one.

(6) Define a server name like docker_[your_project_name]

(7) Add your host on port 80 using Xdebug

(8) Don't forget to click on Use path mappings

(9) We need to add path mapping. Indeed, from the point of view of Xdebug, the PHP application runs in the '/var/www/html' (we are in a container!).But for PHPStorm, my project is in my host machine directory! So we need to map the root of your PHP application to the '/var/www/html' directory, as shown on the image above.

At this point, you should be able to put a breakpoint in your code, start a debugging session and get the application to stop on the breakpoint. Jump to chapter 'Run Xdebug' to see how to use Xdebug.

Xdebug CLI

All applications are not web-based. You might also want to debug a command-line application, from within your container.Let's see how to enable Xdebug for scripts (CLI)

In the docker-compose.yml, just add those environment variables

Remember that we are using our technical director's (David Négrier) images. That's why we can set PHP extensions, PHP .ini configurations into the docker-compose.yml.For more details, please look at this TheCodingMachine Docker PHP images.

(1) Enable the Xdebug extension

Docker

(2) If you are using a different Xdebug port, set your custom Xdebug port

(3) Add your Xdebug server name

(4) If you are using the default Xdebug port, please remove remote_port=[your_xdebug_port]. Otherwise, add your custom Xdebug port

Run Xdebug

Web

Place a breakpoint in your code and launch a debug session

A new windows should open and you should access to PHPStorm debug bar.

Scripts (CLI)

Click on Start Listening for PHP Debug connections

The button should appear like this :

Phpstorm

Place a breakpoint in your code.

Now, just go into your php-apache container using docker exec -ti [container_id] bash and run your script manually.

Addentum: Installing Docker integration in PHPStorm

There is a very useful PHPStorm extension to quickly and efficiently manage your docker containers.

For example:

  • see the logs by container
  • have access to bash in a clique
  • quickly see environment variables
  • clear or restart containers

Before starting

  • You should check that the 'Docker integration' is enabled on your IDE
  • Start the Docker daemon on you machine
  • Move to your favorite Docker project in PHPStorm

Configuring Docker

Now everything should be good, just go to Preferences... > Languages and Frameworks > PHP

(1) Click on ... for openning the remote PHP interpreter

(2) Add a new CLI interpreter by clicking on From Docker, Vagrant, VM, Remote...

(3) Click on Docker (remember that you should start Docker daemon)

(4) Add a new server

(5) Select a server name

(6) Choose how to connect on Docker

(7) Make sure the connection is successful

(8) Select your favorite default image

(9) You need to set the path mapping for your code. By default, it's /opt/project in the container, just change it to /var/www/html

Done! Just click on the Docker button on the bottom of PHPStorm window.

About the author

Alexis is an experienced technical project manager who manages several projects including Eco-Emballages (CITEO). Graduate of EPITECH, Alexis is specialized in PHP frameworks (Symfony, Laravel, Mouf) and he masters Docker.

Related articles

Setting up a XDebug debugging environment for PHP / WAMP / Eclipse PDTJuly 8, 2010

Latest articles

Releasing GraphQLite 4: GraphQL in PHP made easyJanuary 6, 2020
Getting started with TDBM 5.1 and SymfonyAugust 6, 2019

Open-source matters

At TheCodingMachine, we are using open-source tools daily to build our projects. And since we and our clients are ripping huge benefits from these open-source products, we also try to 'give back' to the open-source community.

Freelance Developer?

At TheCodingMachine, we are always looking for top freelance developers to work with us on our next state-of-the-art project.
Interested in developing quality projects? We love good work and we pay fairly.

We are hiring!

Living in France? We are actively looking for developers and project leaders in our Paris and Lyon office.

About us

At TheCodingMachine, we strive to develop websites , extranets, intranets and business web applications in PHP and JavaScript for a wide range of clients.
Do not hesitate to contact us!

Docker

Best practices

When writing code, we expect it to be of the best possible quality. To enforce this quality, we redacted our own 'best practices' described in a dedicated website.
We are happy to share those with you!

Packanalyst

We are the happy parents of Packanalyst, a free online service that lets you search any possible PHP class or interface referenced on Packagist.
Pretty useful for open-source developer to keep an eye on who is using what class/interface of your packages.

Download Xdebug

  • Download the Xdebug extension compatible with your PHP version and install it as described in the installation guide.

    Xdebug 3 brings performance improvements, simplified configuration, and PHP 8 support. To learn more on upgrading to Xdebug 3, see the Upgrade guide.

    If you are using an AMP package, the Xdebug extension may be already installed. Refer to the instructions specific for your package.

Integrate Xdebug with the PHP interpreter

  1. Open the active php.ini file in the editor:

    1. In the Settings/Preferences dialog Ctrl+Alt+S, click PHP.

    2. On the PHP page that opens, click next to the CLI Interpreter field.

    3. In the CLI Interpreters dialog that opens, the Configuration file read-only field shows the path to the active php.ini file. Click Open in Editor.

  2. To disable the Zend Debugger and Zend Optimizer tools, which block Xdebug, remove or comment out the following lines in the php.ini file:

    zend_extension=<path_to_zend_debugger> zend_extension=<path_to_zend_optimizer>
  3. To enable Xdebug, locate or create the [xdebug] section in the php.ini file and update it as follows:

    [xdebug] zend_extension='<path to xdebug extension>' xdebug.remote_enable=1 xdebug.remote_host=127.0.0.1 xdebug.remote_port='<the port (9000 by default) to which Xdebug connects>'
    [xdebug] zend_extension='<path to xdebug extension>' xdebug.mode=debug xdebug.client_host=127.0.0.1 xdebug.client_port='<the port (9003 by default) to which Xdebug connects>'

    In PHP 5.3 and later, you need to use only zend_extension, not zend_extension_ts, zend_extension_debug, or extension.

    To enable multi-user debugging via Xdebug proxies, locate the xdebug.idekey setting and assign it a value of your choice. This value will be used to register your IDE on Xdebug proxy servers.

  4. Save and close the php.ini file.

  5. Verify Xdebug installation by doing any of the following:

    • In the command line, run the following command:

      The output should list Xdebug among the installed extensions:

    • Create a php file containing the following code:

      <?php phpinfo();

      Open the file in the browser. The phpinfo output should contain the Xdebug section:

Configure Xdebug in PhpStorm

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select PHP.

  2. Check the Xdebug installation associated with the selected PHP interpreter:

    1. On the PHP page, choose the relevant PHP installation from the CLI Interpreter list and click next to the field. The list shows all the PHP installations available in PhpStorm, see Configure local PHP interpreters and Configure remote PHP interpreters.

    2. The CLI Interpreters dialog that opens shows the following:
      • The version of the selected PHP installation.

      • The name and version of the debugging engine associated with the selected PHP installation (Xdebug or Zend Debugger). If no debugger is configured, PhpStorm shows the corresponding message:

    Alternatively, open the Installation Wizard, paste the output of the phpinfo(), and click Analyze my phpinfo() output. Learn more about checking the Xdebug installation in Validate the Configuration of a Debugging Engine.

  3. Define the Xdebug behaviour. Click Debug under the PHP node. On the Debug page that opens, specify the following settings in the Xdebug area:

    • In the Debug port field, appoint the port through which the tool will communicate with PhpStorm.

      This must be the same port number as specified in the php.ini file:

      xdebug.remote_port='<the port (9000 by default) to which Xdebug connects>'
      xdebug.client_port='<the port (9003 by default) to which Xdebug connects>'

      By default, Xdebug 2 listens on port 9000. For Xdebug 3, the default port has changed from 9000 to 9003. You can specify several ports by separating them with a comma. By default, the Debug port value is set to 9001,9003 to have PhpStorm listen on both ports simultaneously.

    • To have PhpStorm accept any incoming connections from Xdebug engine through the port specified in the Debug port field, select the Can accept external connections checkbox.

    • Select the Force break at first line when no path mapping specified checkbox to have the debugger stop as soon as it reaches and opens a file that is not mapped to any file in the project on the Servers page. The debugger stops at the first line of this file and Examine/update variables shows the following error message: Cannot find a local copy of the file on server <path to the file on the server> and a link Click to set up mappings. Click the link to open the Resolve Path Mappings Problem dialog and map the problem file to its local copy.

      When this checkbox cleared, the debugger does not stop upon reaching and opening an unmapped file, the file is just processed, and no error messages are displayed.

    • Select the Force break at first line when a script is outside the project checkbox to have the debugger stop at the first line as soon as it reaches and opens a file outside the current project. With this checkbox cleared, the debugger continues upon opening a file outside the current project.

  4. In the External connections area, specify how you want PhpStorm to treat connections received from hosts and through ports that are not registered as deployment server configurations.

    • Ignore external connections through unregistered server configurations: Select this checkbox to have PhpStorm ignore connections received from hosts and through ports that are not registered as deployment server configurations. When this checkbox is selected, PhpStorm does not attempt to create a deployment server configuration automatically.

    • Break at first line in PHP scripts: Select this checkbox to have the debugger stop as soon as connection between it and PhpStorm is established (instead of running automatically until the first breakpoint is reached). Alternatively turn on the Run | Break at first line in PHP scripts option from the main menu.

    • Max. simultaneous connections Use this spin box to limit the number of external connections that can be processed simultaneously.

By default, PhpStorm only listens for incoming IPv4 connections. To enable IPv6 support, you need to make adjustments in PhpStorm JVM options:

  1. Select Help | Edit Custom VM Options from the main menu.

  2. In the .vmoptions file that opens, delete the -Djava.net.preferIPv4Stack=true line.

  3. Restart PhpStorm.

Configure Xdebug for using in the On-Demand mode

PhpStorm supports the On-Demand mode, where you can disable Xdebug for your global PHP installation and have it enabled automatically on demand only when you are debugging your command-line scripts or when you need code coverage reports. This lets your command line scripts (including Composer and unit tests) run much faster.

  1. Disable Xdebug for command-line scripts:

    1. In the Settings/Preferences dialog Ctrl+Alt+S, go to PHP.

    2. From the PHP executable list, choose the relevant PHP interpreter and click next to it. In the CLI Interpreters dialog that opens, click the Open in Editor link next to the Configuration file: <path to php.ini> file. Close all the dialogs and switch to the tab where the php.ini file is opened.

    3. In the php.ini file, find the [xdebug] section and comment the following line in it by adding ; in preposition:

      ;[xdebug] ;zend_extension = '<path to xdebug extension>'
    4. Open the CLI Interpreters dialog and click next to the PHP executable field. PhpStorm informs you that debugger is not installed:

  2. To enable PhpStorm to activate Xdebug when it is necessary, specify the path to it in the Debugger extension field, in the Additional area. Type the path manually or click and select the location in the dialog that opens.

Phpstorm Xdebug Docker Cli

Configure Xdebug for using in the Just-In-Time mode

PhpStorm supports the use of Xdebug in the Just-In-Time (JIT) mode so it is not attached to your code all the time but connects to PhpStorm only when an error occurs or an exception is thrown. Depending on the Xdebug version used, this operation mode is toggled through the following settings:

  • Xdebug 2 uses the xdebug .remote_mode setting, which has to be set to jit.

  • Xdebug 3 uses the xdebug.start_upon_error setting, which has to be set to yes.

The mode is available both for debugging command-line scripts and for web server debugging.

Depending on whether you are going to debug command-line scripts or use a Web server, use one of the scenarios below.

Command-line scripts

For debugging command-line scripts, specify the custom -dxdebug.remote_mode=jit (for Xdebug 2) or -dxdebug.start_upon_error=yes (for Xdebug 3) directive as an additional configuration option:

  1. In the Settings/Preferences dialog Ctrl+Alt+S, navigate to PHP.

  2. From the PHP executable list, choose the relevant PHP interpreter and click next to it.

  3. In the CLI Interpreters dialog that opens, click next to the Configuration options field in the Additional area.

  4. In the Configuration Options dialog that opens, click to add a new entry.

    • For Xdebug 2, type xdebug.remote_mode in the Configuration directive field and jit in the Value field.

    • For Xdebug 3, type xdebug.start_upon_error in the Configuration directive field and yes in the Value field.

    When you click OK, you return to the CLI Interpreters dialog where the Configuration options field shows -dxdebug.remote_mode=jit (for Xdebug 2) or -dxdebug.start_upon_error=yes (for Xdebug 3).

Web server debugging

  1. From the main menu, choose Run | Web Server Debug Validation.

  2. In the Validate Remote Environment that opens, choose the Web server to validate the debugger on.

    • Choose Local Web Server or Shared Folder to check a debugger associated with a local Web server.

      • Path to Create Validation Script: In this field, specify the absolute path to the folder under the server document root where the validation script will be created. For Web servers of the type Inplace, the folder is under the project root.

        The folder must be accessible through http.

      • URL to Validation Script: In this field, type the URL address of the folder where the validation script will be created. If the project root is mapped to a folder accessible through http, you can specify the project root or any other folder under it.

    • Choose Remote Web Server to check a debugger associated with a remote server.

      • Path to Create Validation Script: In this field, specify the absolute path to the folder under the server document root where the validation script will be created. The folder must be accessible through http.

      • Deployment Server: In this field, specify the server access configuration of the type Local Server or Remote Server to access the target environment. For details, see Configure synchronization with a Web server.

        Choose a configuration from the list or click Browse in the Deployment dialog.

  3. Click Validate to have PhpStorm create a validation script, deploy it to the target remote environment, and run it there.

  4. Open the php.ini file which is reported as loaded and associated with Xdebug.

  5. In the php.ini file, find the [xdebug] section.

    Change the value of the xdebug.remote_mode from the default req to jit.

    Change the value of the xdebug.start_upon_error from the default default to yes.

See also Just-In-Time debugging and PHP Exception Breakpoints with PhpStorm and Xdebug

Configure Xdebug running in a Docker container

To configure Xdebug running in a Docker container, provide the Xdebug-specific parameters in the Dockerfile, for example:

RUN pecl install xdebug && docker-php-ext-enable xdebug && echo 'xdebug.remote_enable=on' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && echo 'xdebug.remote_host = host.docker.internal' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

In this example, we're modifying /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini providing the remote_enable and remote_host Xdebug parameters.

Note that the xdebug.remote_host value should be replaced with the IP address of the machine where PhpStorm is running, which is accessible from the Docker container. If you are using Docker for Windows or Docker for Mac, you can set xdebug.remote_host to host.docker.internal, which automatically resolves to the internal address of the host, letting you easily connect to it from the container.

RUN pecl install xdebug && docker-php-ext-enable xdebug && echo 'xdebug.mode=debug' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini && echo 'xdebug.client_host = host.docker.internal' >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

In this example, we're modifying /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini providing the mode and client_host Xdebug parameters.

Note that the xdebug.client_host value should be replaced with the IP address of the machine where PhpStorm is running, which is accessible from the Docker container. If you are using Docker for Windows or Docker for Mac, you can set xdebug.client_host to host.docker.internal, which automatically resolves to the internal address of the host, letting you easily connect to it from the container.

Configure Xdebug running on a Vagrant instance

To configure Xdebug running on a Vagrant instance, connect to the Vagrant machine and provide the Xdebug-specific parameters in the php.ini file:

[xdebug] zend_extension='<path to xdebug extension>' xdebug.remote_enable=1 xdebug.remote_host=10.0.2.2 xdebug.remote_port=9000

Docker Phpstorm Xdebug Nginx

Note that the xdebug.remote_host value is 10.0.2.2. This is the gateway used in the default Vagrant setup, which allows connecting from the instance to host where PhpStorm is running.

[xdebug] zend_extension='<path to xdebug extension>' xdebug.mode=debug xdebug.client_host=10.0.2.2 xdebug.client_port=9003

Phpstorm Docker Compose

Note that the xdebug.client_host value is 10.0.2.2. This is the gateway used in the default Vagrant setup, which allows connecting from the instance to host where PhpStorm is running.