DEV Community

Using Xdebug with Docker

Mert Simsek on April 07, 2019

I am going to show using Xdebug with Docker. Because, as long as you prefer using Xdebug on development workspace with Docker, you have to create r...
Collapse
 
sergeytelpuk profile image
Sergey

I see that you put xdebug config direct in dockerfile it's bad approach. You can create a file
called xdebug.ini it can look like the following:

[Xdebug]
xdebug.remote_autostart=0
xdebug.remote_enable=1
xdebug.default_enable=0
xdebug.remote_host=host.docker.internal
xdebug.remote_port=9000
xdebug.remote_connect_back=0
xdebug.profiler_enable=0
xdebug.remote_log="/tmp/xdebug.log"
xdebug.profiler_output_dir="/var/www/html/profiler"
xdebug.profiler_output_name="cachegrind.out.%p"
xdebug.cli_color=1
xdebug.profiler_append=1

after that copy that one into /usr/local/etc/php/conf.d/

Collapse
 
joppedc profile image
JoppeDC

This is a lot cleaner, also since we use prebuilt docker images we cant just change the dockerfile. I've not been able to get it to work the way you explained, but i'll try again later ;)

Collapse
 
_mertsimsek profile image
Mert Simsek

Thank you for reply, it's prettier solution as you said.

Collapse
 
dvdvdmt profile image
Dmitriy Davydov • Edited

I've used this xdebug.ini placed in /usr/local/etc/php/conf.d/

[Xdebug]
xdebug.remote_enable = true
xdebug.remote_port = 9000
xdebug.remote_host = host.docker.internal

Where host.docker.internal is the proper way to access your host ip address in a containerized app in Docker Desktop clients.

NOTE: Set your debug port in WebStorm to 9000 (Languages & Frameworks > PHP > Debug).

Also you need to add the next line to your Dockerfile or set environment field in docker-compose.yml

ENV PHP_IDE_CONFIG="serverName=<server-name-in-PhpStorm-servers>"

After that you need to press "Start listen on PHP debug connections" (phone icon), set a breakpoint and start a debug session (bug icon). To trigger the debug session you access your API endpoint via GET, POST, with query param ?XDEBUG_SESSION_START=PHPSTORM or cookie set to XDEBUG_SESSION = PHPSTORM. To set this cookie easily you can use Xdebug Helper extension or a similar one.

Collapse
 
adamsandor profile image
Ádám Sándor

Thank you so much for this guide! I was struggling all day to make my Wordpress in Docker on Windows setup work with xdebug. This post had all the missing details like how to have xdebug connect to PhpStorm running on the host.

Collapse
 
_mertsimsek profile image
Mert Simsek

I'm glad to hear that, happy coding :)

Collapse
 
garronarg profile image
Garron Argentina

Why you use the big php.ini like this. Something looks unnecessary.

Collapse
 
_mertsimsek profile image
Mert Simsek

yep, I just wanted the audience to know we can do.

Collapse
 
mohsinsaeed profile image
mohsin-saeed

Hello Dear! In my case ,following your last point, browser keeps loading. But debbuger variables area is not being populated. Can you please help

Collapse
 
_mertsimsek profile image
Mert Simsek

Hi! Is there any error? How can I help you with it?

Collapse
 
mohsinsaeed profile image
mohsin-saeed • Edited

There is no error. But variables are not also being shown in debugger area.

I am not using Symphony, but a simply all code is in index.php file within app/public folder as specified by you. Can there be any mapping issue in this case? My mappings are same as specified by you