I use Docker for development and for my website too. Now it's time to throw away var_dump and to use Xdebug for PHP Docker container. It's quite easy, because I have created a several Docker PHP Xdebug Images on GitHub. You have only to set your path mappings in your IDE. This image can also be used to generate the code coverage with PHPUnit. Ok, let's go step by step.

Pull the PHP Xdebug image from Docker Hub

Ensure Docker is installed and then run the following command:

$ docker pull prooph/php:7.1-cli-xdebug

This can take some minutes. In the meantime, you can configure your IDE path mappings.

Configure path mappings in PhpStorm

Important is the server name application. PhpStorm uses the serverName to determine the right server. xDebug is automatically enabled in the configuration. If you run this Docker container and listen to incoming connections, it stops on the activated breakpoints. Open the settings File -> Settings -> Languages & Frameworks -> PHP -> Server and enable path mappings. Fill out the name with application and set /app to the absolute path mapping on the server of the root folder. Your project files will be mounted there.

PhpStorm Path Mappings

Start the PHP Xdebug Docker container

You have configured your path mappings and enabled listen for incomming Xdebug connections, then you can start the container with the following command from the root of your project.

$ docker run --rm -it --volume $(pwd):/app -e PHP_IDE_CONFIG="serverName=application" prooph/php:7.1-cli-xdebug php [your file]

It's important that your path mapping has the name application and to pass the environment variable PHP_IDE_CONFIG with the name of the server. It is recommended to create a simple bash file which works with every project. Create a file docker-xdebug.sh in your ~/bin folder and put the following lines to it.

#!/bin/bash
docker run --rm -it --volume $(pwd):/app -e PHP_IDE_CONFIG="serverName=application" prooph/php:7.1-cli-xdebug php $@

Make this file executable with chmmod +x ~/bin/docker-xdebug.sh and you can start a new Xdebug session with:

$ docker-xdebug [your PHP file]

Breakpoints doesn't work?

Don't expose the Xdebug port 9000

At default, Xdebug's remote host is configured with xdebug.remote_host=172.17.0.1, but it can be different to your Docker gateway IP address. To check your Docker gateway IP run the command $ ifconfig and look for the docker0 network adapter. Since Docker 1.9, the Docker gateway IP for me is 172.17.0.1. You can override the default Xdebug configuration with the php -d xdebug.remote_host=172.17.0.1 parameter. If you are using Docker Machine and VirtualBox, use the IP of the VirtualBox network interface. Here is an example how to override the default Xdebug remote_host option.

$ docker run --rm -it --volume $(pwd):/app -e PHP_IDE_CONFIG="serverName=application" prooph/php:7.1-cli-xdebug php -d xdebug.remote_host=172.17.0.1 [your PHP file]

Other PHP Versions

The prooph Docker PHP build on Docker Hub includes several PHP versions from PHP 5.6 to 7.2 for CLI and PHP-FPM as well.

Conclusion

Docker is a perfect match for PHP development. Note that these Xdebug settings also work with a web server like nginx and php-fpm. Interested in profiling? Read more about profiling.

Vagrant users can read Vagrant remote PHP & CLI debugging with PHPStorm.