Docker + Xdebug + VSCode Could not connect to client
You can have your docker container connect to your host by configuring PHP appropriately.
Note that I've used Docker's host.docker.internal
domain, that always points to the host IP.
xdebug.remote_enable=1xdebug.remote_port=9000xdebug.idekey=dockerxdebug.profiler_enable=0xdebug.profiler_enable_trigger=1xdebug.remote_host=host.docker.internal
There's no need to open ports to the docker container. Just make sure that your debugging application can listen on the appointed port (9000 in my case) and that this port is not occupied.
Take for example this PhpStorm configuration:
Troubleshooting
Validate that xdebug is installed, run in the php container:
php -i | grep xdebug
Make sure that you're editing the appropriate .ini
files, check:
php -i | grep \.ini
If you have idekey enabled, make sure to configure them in your IDE too:
There's a Debugger Config Validator in PhpStorm if you're exposing PHP scripts on a webserver:
Try to open port 9001
in the docker-compose.yml
by inserting:
ports: - "9001:9001"
Example
services: webserver: build: context: ./bin/webserver container_name: '7.1.x-webserver' restart: 'always' ports: - "80:80" - "443:443" - "9001:9001" links: - mysql volumes: - ${DOCUMENT_ROOT-./www}:/var/www/html - ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini - ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled - ${LOG_DIR-./logs/apache2}:/var/log/apache2
See more about configuring docker-compose.yml: https://docs.docker.com/compose/compose-file/
You might be able to use expose
. We have that in some of our old Dockerfiles which is intended to allow xDebug connections, but I haven't used it personally.
expose: - "9001"
- You should open port 9001 in your host machine, not in Docker.
- Also try to check if Xdebug is configured properly: add phpinfo() to your code and open that page in browser. You shold see xdebug enabled
- If Xdebug enabled, maybe your IDE settings are incorrect