Skip to content

Instantly share code, notes, and snippets.

@mindscratch
Created April 27, 2017 18:04
Show Gist options
  • Save mindscratch/5bfc0578feb7d5d534366c680b422f1b to your computer and use it in GitHub Desktop.
Save mindscratch/5bfc0578feb7d5d534366c680b422f1b to your computer and use it in GitHub Desktop.
Debugging PHP Running in a Docker Container with XDebug and PHPStorm on macOS Sierra

I used docker compose to stand up MariaDB and Apache web server in containers.

xdebug

I'm using php7 with CentOS 7.2. I had to install "php70w-pecl-xdebug.x86_64". I also added the following the Dockerfile

RUN echo "xdebug.idekey = PHPSTORM" >> /etc/php.d/xdebug.ini &&
echo "xdebug.default_enable = 0" >> /etc/php.d/xdebug.ini &&
echo "xdebug.remote_enable = 1" >> /etc/php.d/xdebug.ini &&
echo "xdebug.remote_autostart = 0" >> /etc/php.d/xdebug.ini &&
echo "xdebug.remote_connect_back = 0" >> /etc/php.d/xdebug.ini &&
echo "xdebug.profiler_enable = 0" >> /etc/php.d/xdebug.ini &&
echo "xdebug.remote_host = 10.254.254.254" >> /etc/php.d/xdebug.ini

docker-compose.yml

vesion: '2' services: web: build: . image: web expose: - "9000" ports: - "80:80" environment: PHP_XDEBUG_ENABLED: 1 XDEBUG_CONFIG: remote_host=10.254.254.254 volumes: - .:/app links: - db depends_on: - db

phpstorm

version 2017.1.2

Instead of having to worry about figuring the IP address of my laptop, I set up an alias: sudo ifconfig en0 alias 10.254.254.254 255.255.255.0

In PHP Storm, go to Preferences -> Languages & Frameworks -> PHP -> Servers. Add a server, named "localhost", set the port whichever port Apache is listening on (80 in my case). Set the Debugger to "Xdebug". Check the "Use path mappings", and map the source code from your laptop directory to the directory path in the container. Based on my docker-compose.yml, I map the directory where my source code is to /app in the container, so I configure that in the "Use path mappings" section.

Next, in PHP Storm, go to Preferences -> Languages & Frmaeworks -> PHP -> Debug -> DBGp Proxy. Set IDE key to "PHPSTORM" (this comes from the xdebug.idekey setup in the Dockerfile), set Host to 10.254.254.254 and Port to 9000 (the port we exposed via docker-compse.yml).

Then, follow step 2 to begin listening for debug connections. Set some breakpoints in PHP Storm, load a page in your browser and you're all set!

@matheusfaustino
Copy link

thanks ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment