Search code examples
phpdockerphpstormxdebug

Could not debug an PHP application using PhpStorm which runs in Docker?


My PHP application runs in docker. My IDE is PhpStorm. I configured like this:

PhpStorm configuration

My Docker configuration contains:

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

and docker-compose.yml contains like:

environment:
  -XDEBUG_CONFIG: "remote_host=192.168.0.111 idekey=phpstorm"
  -PHP_XDEBUG_ENABLED: 1 # Set 1 to enable.

And I run my docker as;

docker-compose up.

When I access a page, it is not hitting my breakpoints.

What setting I miss here?

Debug setting in PhpStorm:

enter image description here


Solution

  • It's really hard to get this working. The short answer is that Docker is not for debugging. Use Vagrant.

    Docker is meant to run tiny applications called containers. You want to run the smallest possible process like a database, and then run your http server in another container. Because of this, the standard containers are all bare bones. They're not meant to solve complicated issues. Docker is for production.

    Vagrant, on the other hand, is well-suited to developers. It has a lot of niceties that support the developer and make life easier. It works on Mac, Windows, and Linux, and it runs the same way on all of them so you can easily use this in a team setting by sharing just the Vagrantfile, you get "cloning". It even mounts local folders, and thus gives you real-time updates with your http server. You can also destroy the Vagrant image over and over again which is really nice. A good tip is to record all your setup steps in the Vagrantfile. When you have a good Vagrant setup, destroy the Vagrant image, recreate it, and never touch what's inside of it again. This also really helps when you put a project aside for 6 months and can't remember what you did 6 months ago.