Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xdebug not working in Docker Desktop for Mac

After i switched from Docker Machine to Docker Desktop for Mac, xdebug has stopped working. Port 9000 on the host is unreachable from container with xdebug.
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. :-(

Нow to solve my problem ?

like image 670
shubnikofff Avatar asked Aug 25 '16 11:08

shubnikofff


2 Answers

I have the same problem. It might be related to the limitations of docker within OSX. 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 these is to create a device with a new ip (e.g. 10.254.254.254) that loops back to you localhost. When you then use this ip as remote host address instead the one assigned by docker (either 127.0.0.1 or 172.17.0.2), it should do the trick. Follow this link for a coded solution

like image 159
Danyou Avatar answered Sep 30 '22 14:09

Danyou


Change your docker-compose.yml to the below.

You'll want to expose port 9000, not bind. Also update your xdebug ini to the ip of your host (mac) not the ip of 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 since you may have to update your ip based of moving from wifi to wifi. The xdebug.remote_host= ip should be your mac local network ip. Just remember if you're on apache to do a service apache2 restart or appropriate command to restart your server any time you change the 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
like image 40
DeChamp Avatar answered Sep 30 '22 12:09

DeChamp