[英]Docker + Xdebug + VSCode Could not connect to client
PHP 版本:7.1.20
XDebug 版本:2.7.0
我在 MacOS Mojave 上使用 Docker 容器。 按“F5”后,它会显示调试选项暂停、重新启动和停止。 但 Step Over、Step Into、Step Out 被禁用。
我是个学习者,因为我习惯于在 Windows 操作系统上编码到 2012-13 年。 在那一年之后,我这个月又回到了编码。 :) 即使在 Google 上查看了大量有关如何解决此问题的帖子后,我仍不确定如何最终使其发挥作用。 请帮忙。
我的launch.json
文件:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9001,
"log": true,
"pathMappings": {
"/var/www/html": "${workspaceFolder}/learn"
}
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9001
}
]
}
XDebug php.ini
配置:
xdebug.remote_host = 172.20.0.1
xdebug.remote_port = 9001
xdebug.scream = 1
xdebug.remote_enable = 1
xdebug.show_local_vars = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.remote_log = "/var/www/html/xdebug.log"
xdebug.idekey = "VSCODE"
XDebug 日志文件(来自在 php.ini 中设置 xdebug.remote_log):
[12] Log opened at 2019-05-12 10:16:44
[12] I: Checking remote connect back address.
[12] I: Checking header 'HTTP_X_FORWARDED_FOR'.
[12] I: Checking header 'REMOTE_ADDR'.
[12] I: Remote address found, connecting to 172.20.0.1:9001.
[12] W: Creating socket for '172.20.0.1:9001', poll success, but error: Operation now in progress (29).
[12] E: Could not connect to client. :-(
[12] Log closed at 2019-05-12 10:16:44
调试控制台:
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
这是dockerfile
。
FROM php:7.1.20-apache
RUN apt-get -y update --fix-missing
RUN apt-get upgrade -y
# Install tools & libraries
RUN apt-get -y install apt-utils nano wget dialog \
build-essential git curl libcurl3 libcurl3-dev zip
# Install important libraries
RUN apt-get -y install --fix-missing apt-utils build-essential git curl libcurl3 libcurl3-dev zip \
libmcrypt-dev libsqlite3-dev libsqlite3-0 mysql-client zlib1g-dev \
libicu-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev
# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# PHP Extensions
RUN pecl install xdebug-2.7.0 \
&& docker-php-ext-enable xdebug \
&& docker-php-ext-install mcrypt \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install pdo_sqlite \
&& docker-php-ext-install mysqli \
&& docker-php-ext-install curl \
&& docker-php-ext-install tokenizer \
&& docker-php-ext-install json \
&& docker-php-ext-install zip \
&& docker-php-ext-install -j$(nproc) intl \
&& docker-php-ext-install mbstring \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& pecl install redis \
&& docker-php-ext-enable redis
# Enable apache modules
RUN a2enmod rewrite headers
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
这是docker-compose.yml
文件。
version: "3"
services:
webserver:
build:
context: ./bin/webserver
container_name: '7.1.x-webserver'
restart: 'always'
ports:
- "80:80"
- "443:443"
links:
- mysql
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
mysql:
build: ./bin/mysql
container_name: '5.7-mysql'
restart: 'always'
ports:
- "3306:3306"
volumes:
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: tiger
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'sc-phpmyadmin'
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- '8080:80'
volumes:
- /sessions
redis:
container_name: 'sc-redis'
image: redis:latest
ports:
- "6379:6379"
先感谢您。
您可以通过适当地配置 PHP来让您的 docker 容器连接到您的主机。
请注意,我使用了 Docker 的host.docker.internal
域,它始终指向主机 IP。
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.idekey=docker
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=1
xdebug.remote_host=host.docker.internal
无需打开docker 容器的端口。 只需确保您的调试应用程序可以侦听指定的端口(在我的情况下为 9000)并且该端口未被占用。
故障排除
验证是否安装了 xdebug,在 php 容器中运行:
php -i | grep xdebug
确保您正在编辑适当的.ini
文件,检查:
php -i | grep \\.ini
如果您启用了 idekey,请确保也在您的 IDE 中配置它们:
如果您在网络服务器上公开 PHP 脚本,那么 PhpStorm 中有一个调试器配置验证器:
尝试通过插入以下内容在docker-compose.yml
打开端口9001
:
ports:
- "9001:9001"
例子
services:
webserver:
build:
context: ./bin/webserver
container_name: '7.1.x-webserver'
restart: 'always'
ports:
- "80:80"
- "443:443"
- "9001:9001"
links:
- mysql
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
查看有关配置 docker-compose.yml 的更多信息: https ://docs.docker.com/compose/compose-file/
您也许可以使用expose
。 我们在一些旧的 Dockerfiles 中有它,旨在允许 xDebug 连接,但我没有亲自使用它。
expose:
- "9001"
对于 OP 要求的 VSCode 和 xdebug 3.xx,配置应该是 -
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.start_upon_error=yes
xdebug.client_host=[IP address] <<<< NOTE
xdebug.discover_client_host=true
xdebug.client_port=9000
注意: xdebug.client_host
应该有 vscode 的主机 IP 地址 (192.168...) 而不是host.docker.internal
,这似乎不适用于 vscode。
注 2:不需要为此打开任何端口(上面配置中的 9000)。 这可能是因为 docker 在端口 9000 连接回主机,反之亦然!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.