- For your local dev, create a
Dockerfilethat is based on your production image and simply installxdebuginto it. Exemple:
FROM php:5
RUN yes | pecl install xdebug \
&& echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_enable=on" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_autostart=off" >> /usr/local/etc/php/conf.d/xdebug.ini
- Get you local IP address (
ifconfigor such) - Start your container with the following environment variable:
XDEBUG_CONFIG="remote_host={{YOUR_IP_ADDRESS}}"
-
Simple
dockerrun:docker run -e XDEBUG_CONFIG="remote_host={{YOUR_IP_ADDRESS}}" your-image -
With
docker-compose:# docker-compose.yml foo: build: path/to/Dockerfile environment: XDEBUG_CONFIG: remote_host={{YOUR_IP_ADDRESS}}
- In Intellij/PHPStorm go to:
Languages & Frameworks>PHP>Debug>DBGp Proxyand set the following settings:
Host: your IP addressPort: 9000
Then you're all set and can start listening for PHP Debug connections from your IDE. On the first run it will ask you to map
your local directoryies to the docker directories, but after that nothing will be required anymore!
Happy debugging!
The key here is to now start using
host.docker.internalin yourremote_hostconfiguration moving forward. Also, I thinkremote_connect_backis not necessary and should be removed. I've consolidated a simplified but generic explanation where I try to focus strictly on configuring Xdebug to work in Docker, if anyone is interested:Using Xdebug in Docker (gist)
I did this because even though the core solution for Docker-specific setups is pretty simple (i.e.
remote_host=host.docker.internal), any extra code, configuration details or unrelated bugs can start to make things very confusing for beginners who are diving into Docker for the first time. If anything, it'll at least help me for future reference 😄