简体   繁体   English

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

[英]Docker + Xdebug + VSCode Could not connect to client

PHP version: 7.1.20 PHP 版本:7.1.20

XDebug version: 2.7.0 XDebug 版本:2.7.0

I'm using a Docker container on MacOS Mojave.我在 MacOS Mojave 上使用 Docker 容器。 On pressing "F5", it shows debug options Pause, Restart, and Stop.按“F5”后,它会显示调试选项暂停、重新启动和停止。 But Step Over, Step Into, Step Out are disabled.但 Step Over、Step Into、Step Out 被禁用。

I'm kind of learner because I was used to code till 2012-13 on Windows OS.我是个学习者,因为我习惯于在 Windows 操作系统上编码到 2012-13 年。 After that year, I am back to coding this month.在那一年之后,我这个月又回到了编码。 :) Even after reviewing a lot of posts on Google about how to resolve this issue, I'm not sure how to finally make it work. :) 即使在 Google 上查看了大量有关如何解决此问题的帖子后,我仍不确定如何最终使其发挥作用。 Please help.请帮忙。

My launch.json file:我的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 config: 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 logfile (from setting xdebug.remote_log in php.ini): 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

Debug console:调试控制台:

<- launchResponse
Response {
  seq: 0,
  type: 'response',
  request_seq: 2,
  command: 'launch',
  success: true }

Here's the dockerfile .这是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"]

Here's the docker-compose.yml file.这是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"

Thank you in advance.先感谢您。

You can have your docker container connect to your host by configuring PHP appropriately.您可以通过适当地配置 PHP来让您的 docker 容器连接到您的主机

Note that I've used Docker's host.docker.internal domain, that always points to the host IP.请注意,我使用了 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

There's no need to open ports to the docker container.无需打开docker 容器的端口 Just make sure that your debugging application can listen on the appointed port (9000 in my case) and that this port is not occupied.只需确保您的调试应用程序可以侦听指定的端口(在我的情况下为 9000)并且该端口未被占用。

Take for example this PhpStorm configuration:以这个 PhpStorm 配置为例: 配置

Troubleshooting故障排除

Validate that xdebug is installed, run in the php container:验证是否安装了 xdebug,在 php 容器中运行:

php -i | grep xdebug

Make sure that you're editing the appropriate .ini files, check:确保您正在编辑适当的.ini文件,检查:

php -i | grep \\.ini

If you have idekey enabled, make sure to configure them in your IDE too:如果您启用了 idekey,请确保也在您的 IDE 中配置它们:

配置远程调试

There's a Debugger Config Validator in PhpStorm if you're exposing PHP scripts on a webserver:如果您在网络服务器上公开 PHP 脚本,那么 PhpStorm 中有一个调试器配置验证器:

证实

Try to open port 9001 in the docker-compose.yml by inserting:尝试通过插入以下内容在docker-compose.yml打开端口9001

ports:
 - "9001:9001"

Example例子

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

See more about configuring docker-compose.yml: https://docs.docker.com/compose/compose-file/查看有关配置 docker-compose.yml 的更多信息: https ://docs.docker.com/compose/compose-file/

You might be able to use expose .您也许可以使用expose We have that in some of our old Dockerfiles which is intended to allow xDebug connections, but I haven't used it personally.我们在一些旧的 Dockerfiles 中有它,旨在允许 xDebug 连接,但我没有亲自使用它。

    expose:
      - "9001"
  1. You should open port 9001 in your host machine, not in Docker.您应该在主机中打开端口 9001,而不是在 Docker 中。
  2. Also try to check if Xdebug is configured properly: add phpinfo() to your code and open that page in browser.还要尝试检查 Xdebug 是否配置正确:将 phpinfo() 添加到您的代码中并在浏览器中打开该页面。 You shold see xdebug enabled你应该看到 xdebug 已启用
  3. If Xdebug enabled, maybe your IDE settings are incorrect如果启用了 Xdebug,可能您的 IDE 设置不正确

For VSCode as the OP has asked and xdebug 3.xx, the configuration should be -对于 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

Note: xdebug.client_host should have the host IP address for vscode (192.168...) and not host.docker.internal which does not seem to work for vscode.注意: xdebug.client_host应该有 vscode 的主机 IP 地址 (192.168...) 而不是host.docker.internal ,这似乎不适用于 vscode。

Note 2: There is no need to open any port (9000 in the config above) for this purpose.注 2:不需要为此打开任何端口(上面配置中的 9000)。 This could be because docker connects back to host at port 9000 and not vice versa!这可能是因为 docker 在端口 9000 连接回主机,反之亦然!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM