gpt4 book ai didi

php - Docker + Xdebug + VSCode 无法连接到客户端

转载 作者:IT老高 更新时间:2023-10-28 21:26:02 25 4
gpt4 key购买 nike

PHP 版本:7.1.20

XDebug 版本:2.7.0

我在 MacOS Mojave 上使用 Docker 容器。按“F5”时,它会显示调试选项暂停、重新启动和停止。但是 Step Over, Step Into, Step Out 被禁用。

我是个学习者,因为直到 2012-13 年我都习惯在 Windows 操作系统上编写代码。那年之后,我这个月又开始编码了。 :) 即使在谷歌上查看了很多关于如何解决这个问题的帖子之后,我也不确定如何最终让它发挥作用。请帮忙。

我的 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)并且该端口未被占用。

以这个 PhpStorm 配置为例: Configuration

疑难解答

验证xdebug是否安装,在php容器中运行:

php -i | grep xdebug

确保您正在编辑适当的 .ini 文件,检查:

php -i | grep\.ini

如果您启用了 idekey,请确保也在您的 IDE 中配置它们:

config remote debug

如果您在网络服务器上公开 PHP 脚本,PhpStorm 中有一个调试器配置验证器:

Validate

关于php - Docker + Xdebug + VSCode 无法连接到客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56099086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com