Debug PHP in Docker using Xdebug with VS Code or PhpStorm

Debug PHP in Docker using Xdebug with VS Code or PhpStorm

I'm tired of reading countless of tutorials about this, many of which include a lot of unnecessary steps and do not work on all platform.

To put an end to this, I published a repo: php-xdebug-docker with everything that is necessary to get Xdebug to work with VS Code and PhpStorm when running the PHP code inside a Docker container, including a tutorial for both editors.

The Dockerfile is based on the php:7-apache image. It installs and configure Xdebug. The most important parameters are xdebug.remote_host and xdebug.idekey, which I commented inside the Dockerfile.

I included a docker-compose.yml so you can inspire yourself from it, but also get and up in running easily to make sure everything works.

Visual Studio Code
PhpStorm

Enjoy 👨‍💻

Stanislas
Author
Stanislas
I like building things with code and computers

Comments

2Atom feed
Markdown supported
  1. Pol

    Thanks for sharing. But for linux, the ip -4 route list match 0/0 | awk '{print $3 " host.docker.internal"}' >> /etc/hosts \ command is not persistent (more details in https://stackoverflow.com/questions/38302867/how-to-update-etc-hosts-file-in-docker-image-during-docker-build) after the build so the address is lost. A workaround is to add the extra_hosts directive in the docker-compose file:

    extra_hosts: - host.docker.internal:${DOCKER0_IP} With DOCKER0_IP defined in the .env file or set manually.