【发布时间】:2021-11-03 23:17:35
【问题描述】:
我认为我非常接近(我希望)让 xdebug 在 docker 容器中运行,旨在通过 Visual Studio Code 进行连接。
我认为也许我应该向容器中的 /etc/hosts 文件添加一个配置,将 IP 地址定向到我的文件正在提供的 url,但我不确定该 IP 地址应该是什么对应。
错误:
通过xdebug_info()(和/tmp/xdebug.log):
[Step Debug] Could not connect to debugging client. Tried: localhost:9003 (through xdebug.client_host/xdebug.client_port) :-(
通过telnet localhost 9003:
Trying 127.0.0.1...
Trying ::1...
telnet: Unable to connect to remote host: Cannot assign requested address
xdebug.ini 文件:
zend_extension=xdebug.so
[xdebug]
xdebug.mode=debug
xdebug.start_with_request = yes
xdebug.discover_client_host = 0
xdebug.remote_connect_back = 1
xdebug.client_port = 9003
xdebug.force_error_reporting = 1
xdebug.remote_host=host.docker.internal
xdebug.idekey=VSCODE
xdebug.mode = debug
xdebug.log=/tmp/xdebug-local.log
launch.json:
"version": "0.2.0",
"configurations": [
{
"type": "php",
"request": "launch",
"name": "xDebug listen",
"port": 9003,
"url": "http://mzmbo.test",
"stopOnEntry": true,
"pathMappings": {
"/var/www/html/wp-content/plugins/my-wp-plugin": "${workspaceRoot}/my-wp-plugin"
}
}
]
}
Dockerfile:
FROM "wordpress:${WP_VERSION:-latest}"
RUN apt-get update -y \
&& apt-get install -y \
libxml2-dev \
vim \
lsof \
ufw \
telnet \
&& apt-get clean -y \
&& ufw allow 9003 \
&& docker-php-ext-install soap \
&& docker-php-ext-enable soap \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug
# Replace php.ini
COPY php.ini /usr/local/etc/php
COPY docker-php-ext-xdebug.ini /usr/local/etc/php/conf.d
终于docker-compose.yml:
version: "3"
services:
wordpress:
build: .
environment:
VIRTUAL_HOST: "myproject.test"
WORDPRESS_DB_HOST: "mysql"
WORDPRESS_DB_NAME: "wordpress"
WORDPRESS_DB_PASSWORD: ""
WORDPRESS_DB_USER: "root"
depends_on:
- "mysql"
networks:
- "front"
- "back"
volumes:
- "wp:/var/www/html:rw"
- "./my-wp-plugin:/var/www/html/wp-content/plugins/my-wp-plugin:ro"
xdebug_info() 的结果显示:
Step Debugger ✔ enabled
xdebug.client_host localhost
xdebug.client_port 9003
xdebug.start_with_request yes
我打赌我需要添加配置,但不确定是什么。
更新
从this tutorial,我看到xdebug.discover_client_host=true“告诉Xdebug 尝试从HTTP 请求中提取客户端的IP”,如果失败则回退到client_host。 (它检查$_SERVER['HTTP_X_FORWARDED_FOR'] 和$_SERVER['REMOTE_ADDR'] 变量以找出要使用的IP 地址。)这是替换xDebug 2 的remote_connect_back 的配置。 xDebug 的大部分significant configs have changed 在v2 和v3 之间。
但仍然出现相同的连接错误:
Trying 192.168.16.1...
telnet: Unable to connect to remote host: Connection refused
在日志中:
[21] [Step Debug] WARN: Could not connect to client host discovered \
through HTTP headers, connecting to configured address/port: localhost:9003. :-|
[21] [Step Debug] WARN: Creating socket for 'localhost:9003', \
poll success, but error: Operation now in progress (29).192.168.16.1:9003 (from HTTP_X_FORWARDED_FOR HTTP header), \
localhost:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(
我尝试将指向 192.168.16.1 的 /etc/hosts 条目添加到 myproject.test(在 Docker 容器中),但还没有弄清楚如何刷新容器中的 DNS 记录。也许我需要在容器中添加extra host mapping?
看到一个建议Lando 的帖子,它似乎包裹了 Docker,可以减轻一些痛苦,但我还没有准备好放弃这个,所以如果你读过这篇文章,非常感谢你远。
以下解决方案(感激地)接受。
我还需要更改 VS Code launch.json 文件:
"stopOnEntry": false
因为我的仓库中没有 WP 代码库,只有一个特定的插件。使用"stopOnEntry": true,xDebug 插件在"${workspaceRoot} 中寻找(不存在的)index.php 文件(起点),而不是仅检查断点,这是我需要的,我的本地代码库只是整个应用程序的部分。
另外,我认为接受的答案中的 # comments 使 .ini 文件无效,所以如果您的设置似乎没有出现,请注意这一点。 p>
【问题讨论】:
标签: php docker docker-compose telnet xdebug-3