Sep 22, 2016 PHP 中文版
As more and more PHP developers utilize Docker in development and production environment, debugging becomes more complex. Thus, this post is about how to debug PHP application in Docker by using Xdebug with Atom and php-debug.
Before installation of Xdebug, make sure you open the port which Xdebug needs when you create a container. Like this:
# If Xdebug needs port 9000, then add -p 9000:9000 in the command
docker run -d \
-p 80:80 \
-p 9000:9000 \
...
Let’s install Xdebug in your container:
# Enter into your container
docker exec -it <your-container-name> bash
# Install Xdebug via pecl
pecl install xdebug
Then put Xdebug settings at the end of php.ini (e.g. /usr/local/etc/php/php.ini).
# Skip some content ...
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so
xdebug.remote_enable=1
# Put the IP of Docker container below
xdebug.remote_host=192.168.99.100
xdebug.remote_connect_back=1
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart=true
Don’t forget to restart the container to make sure Xdebug can work correctly. To restart:
docker restart <your-container-name>
First, we have to install php-debug for Atom. To install it via apm:
# Install php-debug
apm install php-debug
# Enable php-debug
apm enable php-debug
Or, you can install the package through Settings. After installation, open the Atom’s config (config.cson) and put php-debug settings in it:
"*":
"php-debug":
PathMaps: [
"/path/to/app/in/docker;/path/to/app/in/local"
]
ServerPort: 9000
welcome:
showOnStartup: false
How to debug? Open your file, and then press fn + alt + F9 at the line where you want to insert breakpoints. After that, press ctrl + alt + D to open the php-debug window. You can see it shows “Listen on port 9000 …”. Now, it’s ready and all you have to do is to reload a page or rerun a script. All variables or related information will be revealed in that window.
If you have any suggestions, questions or even find some typos, feel free to contact me. Thank you! :)
zeckli.devforgalaxy@gmail.com © 2015-2019 zeckli, thanks to Jekyll and GitHub.