Errors are inevitable while developing software applications. If left unattended, these errors can have critical consequences on users and developers. In 2020, a study commissioned by the Consortium for Information & Software Quality (CISQ) concluded that software errors cost US organizations about $2.08 trillion.
To eliminate these costs, software professionals should never overlook testing & debugging. During and post-production, they can run an automated test suite to ensure that their products are free of errors & work as anticipated.
Likewise, in Dockers, you have to run commands to create & manage containers & run a complex application to test & carry out the debugging process.
But as said, every path has its puddle; Docker also has complexity while managing the project configurations with all the dependencies. Earlier debugging applications from your IDE was straightforward in a typical local development environment. But now, Xdebug requires some additional configuration mechanisms.
To help you out, this blog will take you step-by-step through the installation and configuration process of Xdebug in PHPStorm with a Dockerized Symfony 4 application.
Install Xdebug on your Docker container. The way to do this will depend on your base image. I always use alpine-based images. Refer to this Dockerfile to know how you can dockerize a Symfony application, as included in the demo repository.
Here is the relevant excerpt of the 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 ;
I don't want to have a separate Dockerfile for development and production, so I have defined a build argument that will tell whether to install Xdebug or not.
Next, on my Docker-compose file, I have the following definition for my application-
version: "3"
services:
php:
build:
context: .
args:
- WITH_XDEBUG=true
env_file: .env
volumes:
- .:/var/www/app:rw
Learn more on the complete docker-compose file.
The essential bit is the "env_file" instruction which tells Compose to load environment variables from a ".env" file. It is the standard way for Symfony 4 applications.
We will use that file to add some required environment variables for Xdebug. If you prefer it, you can also add directly to the docker-compose file using the "environment" section.
We will define the following environment variables-
Note- It is mandatory to enable these two settings to facilitate its working.
We will add them to our ".env" file like this-
And that's it in terms of code.
Next, let's dig into PHPStorm configurations.
The first thing you should do is to check your Debug settings. In PHPStorm, go to File -> Settings -> Languages and Frameworks -> PHP > Debug.
Make sure you have the same port that you configured previously in the "XDEBUG_CONFIG" environment variable.
Next, we need to configure a server. That is how PHPStorm will map the file paths in your local system to the ones in your container.
Go to File -> Settings -> Languages and Frameworks -> PHP -> Servers
8. Click on the green "plus" sign at the top left and select "PHP Remote Debug" from the list.
Now configure it like this:
Make sure you associate it with the previously created "server" definition. Use "PHPSTORM" as the IDE key to configure it appropriately.
Once it is set, we can move to the testing.
If you have followed the steps carefully, you will see the execution stop at your breakpoint.
And that's it. You have a fully configured development environment now with Docker and Xdebug integrated with PHPStorm IDE.
Happy debugging :)