[英]xdebug not working in Docker Desktop for Mac
在我從Docker Machine切換到Docker Desktop for Mac 后,xdebug 停止工作。 使用 xdebug 無法從容器訪問主機上的端口9000
。
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如何解決我的問題?
我也有同樣的問題。 它可能與OSX中docker的局限性有關。 請參閱這些鏈接。
https://docs.docker.com/docker-for-mac/networking/ https://forums.docker.com/t/explain-networking-known-limitations-explain-host/15205
還提出了可能的解決方法。 其中之一是創建一個帶有新ip的設備(例如10.254.254.254),它會循環回到localhost。 然后,當您使用此ip作為遠程主機地址而不是docker(127.0.0.1或172.17.0.2)分配的地址時,它應該可以解決問題。 請點擊此鏈接獲取編碼解決方案
將docker-compose.yml更改為以下內容。
您將要公開端口9000,而不是綁定。 還要將xdebug ini更新到主機(mac)的ip而不是docker的ip。
我還添加了如何將mac中的xdebug文件直接掛載到docker中,以便您可以即時更新它。 這可以讓你更加控制,因為你可能需要根據從wifi到wifi的更新你的IP。 xdebug.remote_host = ip應該是你的mac本地網絡ip。 只要記住你是否在使用apache來執行service apache2 restart
或適當的命令,以便在你更改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
我花了一些時間來解決這個問題,在閱讀官方文檔后我找到了一個更簡單的解決方案: https: //docs.docker.com/docker-for-mac/networking/#httphttps-proxy-support這部分 :
我想從一個集裝箱連接到主機上的服務
主機具有更改的IP地址(如果您沒有網絡訪問權限,則為無)。 從18.03開始,我們建議連接到特殊DNS名稱host.docker.internal,該名稱解析為主機使用的內部IP地址。 這是出於開發目的,不適用於Docker for Mac之外的生產環境。
一旦理解了這一點,就可以在容器內的php.ini中將remote_host
設置為host.docker.internal
。 另外,不要忘記將xdebug.remote_connect_back
設置為0,不忽略主機設置:
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
我使用了這個設置並且它起作用了:)
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM
xdebug.remote_host=host.docker.internal
xdebug.remote_enable=1
xdebug.remote_connect_back=0
在 vscode 中使用 launch.json
"name": "Listen 9000",
"type": "php",
"request": "launch",
"log": true,
"externalConsole": false,
"pathMappings": {
"/var/www/html": "/Users/folder/project/src"
},
"port": 9000,
使用 docker-compose.yml:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.