Add the following xdebug.ini in your docker container:
zend_extension=/usr/lib/php/20170718/xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_connect_back=off
xdebug.idekey=debugit
xdebug.remote_host=docker.for.mac.localhost
The most important thing is to set xdebug.remote_connect_back=off
and to set xdebug.remote_host=docker.for.mac.localhost
.
If you want to know why check out this discussion on docker forum.
After that, Configure Docker for Mac integration on PHPStorm.
- Go to
Preferences -> Build, Execution & Deployment > Docker
- Click on
+
icon somewhere. - Select
Docker for Mac
- Add path mappings of your container path to local path on Mac.
- Click OK.
After that, Add Docker run configuration for the project as
- Go to
Run > Edit Configurations
- Click on
+
icon somewhere. - Select
Docker > docker-compose
. - Choose the server that you just created while configuring Docker integration on PHPStorm.
- Add the path to the compose file.
- If necessary, add services.
- Click OK.
After that, Add Debug configuration as
- Go to
Run > Edit Configurations
- Click on
+
icon somewhere. - Select
PHP Remote Debug
. - Enter an appropriate name. I choose
Xdebug Docker
- Check
Filter debug connection by IDE key
. - Select the docker server.
- Add the value
debugit
(set inxdebug.idekey
above) as IDE key(session id). - Click OK.
After that, now start debugging as:
- Go to
Run > Run
- Select the name of Debug configuration. For me, it was
Xdebug Docker
. - In the toolbar, Click a phone-like icon which says
Start listening for PHP Debug connections
.
Happy Debugging!!!