Xdebug not working in Docker for Mac

After switching from Docker Machine to Docker for Mac, xdebug stops working. Port 9000 on the host is not accessible from the xdebug container.
php.ini :

xdebug.remote_enable=1 xdebug.remote_port=9000 xdebug.remote_host=172.18.0.1 xdebug.idekey=PHPSTORM 

docker-compose.yml :

 version: '2' services: php: image: <image name> ports: - 80:80 # - 9000:9000 volumes: - .:/var/www/html - ./php.ini:/usr/local/etc/php/conf.d/php.ini 

xdebug.log :

 I: Checking remote connect back address. I: Checking header 'HTTP_X_FORWARDED_FOR'. I: Checking header 'REMOTE_ADDR'. I: Remote address found, connecting to 172.18.0.1:9000. E: Could not connect to client. :-( 

How to solve my problem?

+7
docker xdebug macos docker-for-mac
source share
2 answers

I have the same problem. This may be due to OSX docker restrictions. See these links.

https://docs.docker.com/docker-for-mac/networking/ https://forums.docker.com/t/explain-networking-known-limitations-explain-host/15205

Possible workarounds have also been suggested. One of them is to create a device with a new ip (for example, 10.254.254.254), which returns to you on localhost. When you then use this ip as the address of the remote host, and the one that is assigned by the docker (127.0.0.1 or 172.17.0.2) should do the trick. Follow this link for a coded solution.

+7
source share

Modify your docker-compose.yml file below.

You want to open port 9000 and not bind it. Also upgrade your xdebug ini to the ip of your host (mac), not ip docker.

I also added how you can mount the xdebug file from your Mac directly to your docker so you can update it on the fly. This allows you more control, as you may have to update your ip based on the transition from Wi-Fi to Wi-Fi. Xdebug.remote_host = ip should be your mac local network. Just remember, if you use apache to execute service apache2 restart or the corresponding command to restart your server anytime you change ip.

 version: '2' services: php: image: <image name> ports: - 80:80 expose: - "9000" volumes: - .:/var/www/html - ./php.ini:/usr/local/etc/php/conf.d/php.inivolumes: - ./20-xdebug.ini:/etc/php/7.1/cli/conf.d/20-xdebug.ini //obviously you would change this to your correct paths - ./20-xdebug.ini:/etc/php/7.1/apache2/conf.d/20-xdebug.ini //obviously you would change this to your correct paths # 20-xdebug.ini, this is how mine is setup. zend_extension = /usr/lib/php/20160303/xdebug.so xdebug.remote_enable=1 xdebug.remote_host=192.168.0.4 // Make sure you use your host (mac) local ip, not the ip of docker. xdebug.remote_port=9000 xdebug.idekey = PHPSTORM xdebug.remote_handler = dbgp xdebug.remote_autostart = 1 xdebug.remote_log = /var/log/xdebug.log 
+1
source share

All Articles