Debugging PHP in a Docker container with PhpStorm
Getting debugging to work with PHP running inside a Docker container can be a hassle. This post is a step-by-step guide on setting up debugging PHP in a Docker container with PHPStorm. This guide works for any PHP framework and library, no matter if your project is running vanilla PHP, CodeIgniter or another setup. You can find a working example project here [1].
To debug your PHP project with PhpStorm, follow these steps:
-
Create a directory
docker
with the fileserror_reporting.ini
andxdebug.ini
.Directorydocker
- error_reporting.ini
- xdebug.ini
Directorysrc
- […]
- Dockerfile
- docker-compose.yaml
- […]
-
Mount the two config files you just created into the Docker container by modifying your
docker-compose.yaml
file.Your compose file my look different of course. The important thing is to add the three lines indicated just below.
-
Make your host IP address available inside the container via
host.docker.internal
by further modifying yourdocker-compose.yaml
file. -
Install and enable xdebug in the Docker container by adding the following
RUN
instruction to yourDockerfile
.Your Dockerfile may look different of course. The important thing is just to add
RUN pecl install xdebug && docker-php-ext-enable xdebug
. -
Make sure PhpStorm debug settings are configured correctly.
-
Configure path mappings between PhpStorm and the Docker container.
-
Create a PhpStorm PHP Remote Debug configuration.
-
Run the PhpStorm PHP Remote Debug configuration and make sure PhpStorm is listening for incoming connection.
-
Install an Xdebug browser addon, configure the IDE key and enable debugging.
Refer to this table by JetBrains. For this guide, I will use Firefox and Xdebug Helper.
-
Set a breakpoint and enjoy your debugging experience!