shubnikofff
shubnikofff

Reputation: 113

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 ?

Upvotes: 11

Views: 7644

Answers (4)

Guru
Guru

Reputation: 928

i used this setting and it worked :)

xdebug.remote_port=9000
xdebug.idekey=PHPSTORM
xdebug.remote_host=host.docker.internal
xdebug.remote_enable=1
xdebug.remote_connect_back=0

with launch.json in vscode

 "name": "Listen 9000",
 "type": "php",
 "request": "launch",
 "log": true,
 "externalConsole": false,
 "pathMappings": {
        "/var/www/html": "/Users/folder/project/src"
     },
  "port": 9000,

With docker-compose.yml:

enter image description here

Upvotes: 0

mbritto
mbritto

Reputation: 1117

I've struggled with this for a bit of time and I've found a simpler solution after reading the official documentation at https://docs.docker.com/docker-for-mac/networking/#httphttps-proxy-support Especially this part :

I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker for Mac.

Once you've understand this you can set the remote_host setting to host.docker.internal in your php.ini inside the container. Also don't forget to set the xdebug.remote_connect_back to 0 the the host settings is not ignored :

xdebug.remote_port=9000
xdebug.idekey=PHPSTORM
xdebug.remote_log=/tmp/xdebug.log
xdebug.remote_host=host.docker.internal
xdebug.remote_enable=1
xdebug.remote_connect_back=0

Upvotes: 0

DeChamp
DeChamp

Reputation: 31

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

Upvotes: 1

Danyou
Danyou

Reputation: 190

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

Upvotes: 12

Related Questions