Bobcares

Xdebug docker phpstorm | Complete guide

by | Sep 2, 2022

From this Xdebug docker phpstorm article, you will be setting up a development environment with Docker and Xdebug. As a part of our Docker hosting support service, Bobcares give you detailed note about docker queries.

Xdebug docker phpstorm Overview

Docker has complexity while managing the project configurations with all the dependencies. In Dockers, to create, manage containers & run a complex application you need to run commands also similar to testing and carrying out the debugging process command running is important. It is said as every path has its own puddle.

xdebug docker phpstorm

Earlier debugging applications from your IDE was direct work in a local development environment. However, now Xdebug requires some additional configuration mechanisms.

 

To help you out, this blog will take you step-by-step procedure of the installation and configuration process of Xdebug docker phpstorm with a Dockerized application.

 

Step 1 – Dockerize the Application

 

Initially install Xdebug on your Docker container. The way to do this will depend on your base image, it is suggested to use alpine-based images. You can check on the snippet of Dockerfile that installs Xdebug:

 
ARG WITH_XDEBUG=false
RUN if [ $WITH_XDEBUG = "true" ] ; then \

            pecl install xdebug; \

            docker-php-ext-enable xdebug; \

            echo "error_reporting = E_ALL" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \

            echo "display_startup_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \

            echo "display_errors = On" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \

            echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini; \

        fi ;
 

This can be defined as a build argument that will define whether to install Xdebug or not.

 

Next, on my Docker-compose file, we have the following definition for the application-

 
version: "3"

services:

  php:

    build:

      context: .

      args:

        - WITH_XDEBUG=true

    env_file: .env

    volumes:

      - .:/var/www/app:rw
 

From the given, the essential bit is said to be “env_file” which tells the Compose to load environment variables from a “.env” file. This is the standard way for Symfony 4 applications.

 

We will further use that file to add a few required environment variables for Xdebug and you can also configure directly to the docker-compose file using the “environment” section.

 

Environment Variables

 

We will define the following environment variables-

 
  • PHP_IDE_CONFIG – Defines the server configuration associated with the application.
 
  • XDEBUG_CONFIG – This variable defines Xdebug configurations. Basically, the “remote host” is the private IP of your host machine and the “remote_port” is the port that PHPStorm will be listening for incoming Xdebug connections. These two configs allow PHPStorm and Xdebug to communicate effectively. 
 

It is recommended to enable these two settings to facilitate its working, we are going to add them to our “.env” file as shown:

PHP_IDE_CONFIG=serverName=symfony-demo XDEBUG_CONFIG=remote_host=192.168.1.102 remote_port=9001
 

Step 2 – PHPStorm configurations

 

We next need to check on Debug settings. In PHPStorm, access to File >> Settings >> Languages and Frameworks >> PHP >> Debug.

 

Make sure that the same port is configured previously in the “XDEBUG_CONFIG” environment variable.

 

Next, we need to configure the server, that is how PHPStorm will map the file paths in the local system to the container.

 

Go to File >> Settings >> Languages and Frameworks >> PHP >> Servers.

 
  • Give a name to the server. Make sure to match the given server name with the value you have defined in your “PHP_IDE_CONFIG” environment variable. We will call it “Symfony-demo.”
 
  • Both the “host” and “port” will enable access to your application.
 
  • And then the “Path mappings.”
 
  • In the “Project files” section, you have to now map the root path of your application to the path inside the container. In my case it’s “/var/www/app”.
 
  • Click “Apply” to save your configurations.
 
  • The last part is to configure the remote debugger of your project >> On the top right, click on “edit configurations” >> Click on the green “plus” sign at the top left and select “PHP Remote Debug” from the list.
 

Further, you need to associate it with the previously created “server” definition. Use “PHPSTORM” as the IDE key to configure them. Once this is completed we will move on to the Testing part.

 

Step 3 – Testing

 
  • Firstly Open the “src/Controllers/HelloController.php” and place a breakpoint to the “hello” method.
 
  • Start your Docker container with docker-compose up.
 
  • Then on the top right corner of PHPStorm click to the “Start Listening for PHP Debug connections” icon.
 
  • Open – http://localhost:8888?XDEBUG_SESSION_START=PHPSTORM

 

Finally, you will get to see an execution stop at your breakpoint and that’s it. You have fully successfully configured your development environment with Docker and Xdebug integrated with PHPStorm IDE.

 

[Need assistance with similar queries? We are here to help]

 

Conclusion

 

To conclude from Xdebug docker phpstorm article,earlier debugging applications from IDE were elementary in a typical local development environment. But now, Xdebug requires certain additional configuration mechanisms.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.