Docker 无法分配请求的地址

Docker Cannot assign requested address

我认为我非常接近(我希望)在 docker 容器中安装 xdebug 运行,旨在通过 Visual Studio 代码连接。

我想也许我应该向容器中的 /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, I see that xdebug.discover_client_host=true "tells Xdebug to attempt to extract the IP of the client from the HTTP request", falling back to client_host if that fails. (It checks $_SERVER['HTTP_X_FORWARDED_FOR'] and $_SERVER['REMOTE_ADDR'] variables to find out which IP address to use.) This is the config that replaces xDebug 2's remote_connect_back. Most of xDebug's significant configs have changedv2v3 之间。

仍然出现相同的连接错误,但是:

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) :-(

我尝试添加一个 /etc/hosts 入口指向 192.168.16.1myproject.test(在 Docker 容器中)但还没有弄清楚如何刷新 DNS 记录容器。也许我需要在容器中添加

遇到了一个 post,它建议 Lando,它似乎包裹着 Docker,可以缓解一些这种痛苦,但我还没有准备好放弃这个, 如果您已经读到这里,非常感谢。

以下解决方案(感激地)被接受。

我还需要对 VS Code launch.json 文件进行更改:

"stopOnEntry": false

因为我的仓库中没有 WP 代码库,只有一个特定的插件。使用 "stopOnEntry": true,xDebug 插件在 "${workspaceRoot} 中寻找(不存在的)index.php 文件(起点),而不是仅仅检查断点,这正是我所需要的,我的本地代码库只是整个应用程序的 部分

此外,我 认为 接受的答案中的 # comments 使 .ini 文件无效,所以如果它看起来像你的,请注意这一点设置未出现。

看起来您正在使用 Xdebug 3,而您的一些配置参数来自已弃用的 Xdebug 2(remote_hostremote_connect_back 与新的 discover_client_host 冲突)- 请参阅 https://xdebug.org/docs/all_settings。让我们从清理这些开始:

zend_extension=xdebug.so

[xdebug]
; Set the mode to debug
xdebug.mode=debug
; Trigger the connection on each request/CLI script execution
xdebug.start_with_request=yes
; Do not try to connect to the host reported by HTTP headers, use the one in client_host instead
xdebug.discover_client_host=0
; Force connect to the Docker host IP
xdebug.client_host=host.docker.internal
xdebug.idekey=VSCODE
xdebug.log=/tmp/xdebug-local.log

这些设置应该强制 Xdebug 始终连接到您的 Docker 主机 IP。根据您的 Docker 版本和环境,您可能需要将以下设置添加到 docker-compose.yml:

extra_hosts:
  - "host.docker.internal:host-gateway"