阻止从Docker映像到主机的连接(使用Intellij的Docker容器中的PHP和Xdebug)

发布于 2025-02-01 18:02:15 字数 3317 浏览 4 评论 0原文

Docker Image内部运行的Xcode无法连接到客户端:

Xdebug: [Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port)

设置如下:

  • docker-compose.yml

     版本:'3'
    
    服务:
      Apache:
        container_name:apache
        extra_host:
           - “ host.docker.internal:主机门路”
        建造:
          上下文:./ local
          Dockerfile:./apache.dockerfile
        卷:
           -  ./httpdocs://var/www/html/
           -  ./local/xdebug.ini:/usr/local/etc/php/php/conf.d/docker-php-ext-ext-xdebug.ini
        端口:
           - “ 8080:80”
     
  • apache.dockerfile

     来自php:8.0-apache-bullseye
    
    运行A2enmod重写
    
    运行docker-php-ext-install -j $(nproc)mysqli
    运行docker-php-ext-install -j $(nproc)PDO
    运行docker-php-ext-install -j $(nproc)pdo_mysql
    运行docker-php-ext-install -j $(nproc)exif
    
    运行PECL安装Xdebug&& docker-php-ext-enable xdebug
     
  • xdebug.ini

      zend_extension = xdebug.so
    xdebug.mode =开发,调试
    xdebug.start_with_request = yes
    xdebug.discover_client_host = 0
    xdebug.client_host = host.docker.internal
     
  • docker版本

     客户端:Docker Engine-社区
    版本:20.10.16
    API版本:1.41
    GO版本:GO1.17.10
    git commit:AA7E414
    建造:5月12日星期四:17:30 2022
    OS/Arch:Linux/AMD64
    上下文:默认值
    实验:是真的
    
    服务器:Docker Engine-社区
    引擎:
      版本:20.10.16
      API版本:1.41(最小版1.12)
      GO版本:GO1.17.10
      GIT提交:F756502
      构建:5月12日至09:15:33 2022
      OS/Arch:Linux/AMD64
      实验:错误
    容器:
      版本:1.6.4
      GitCommit:212E8B6FA2F44B9C21B2798135FC6FB7C53EFC16
    runc:
      版本:1.1.1
      GitCommit:V1.1.1.1-0-G52DE29D
    Docker-Init:
      版本:0.19.0
      GitCommit:DE40AD0  
     
  • 主机:linux tuxmobile02 5.15.0-33-加生物#34-ubuntu smp smp wed 5月18日13:34:26 UTC 2022 x86_64 x86_64 x86_64 x86_64 x864 x86_64 gnu/linux gnu/linux

  • intellij 2022.1,设置>语言&框架> php>调试

    • xdebug端口:9003
    • [x]可以接受外部连接
  • ufw是禁用的:

      $ UFW状态
    状态不活跃
     

Xdebug Client(Intellij的一部分)正在运行并可以从 host

$ nmap -p 9003 localhost
Starting Nmap 7.80 ( https://nmap.org ) at 2022-05-27 10:32 CEST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000049s latency).

PORT     STATE SERVICE
9003/tcp open  unknown

Nmap done: 1 IP address (1 host up) scanned in 0.03 seconds

但是它无法从 docker容器

$ docker exec -it apache /bin/bash
root@cf85dafe5c66:/var/www/html# nmap -p 9003 localhost
Starting Nmap 7.80 ( https://nmap.org ) at 2022-05-27 08:33 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000041s latency).
Other addresses for localhost (not scanned): ::1

PORT     STATE  SERVICE
9003/tcp closed unknown

Nmap done: 1 IP address (1 host up) scanned in 0.07 seconds

文件/etc/etc/etc/ Docker映像的主机看起来如预期:

# cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.1  host.docker.internal
172.19.0.3  cf85dafe5c66

=>知道什么可能导致从Docker映像到主机的阻塞连接?

Xcode running inside Docker Image is unable to connect to the client:

Xdebug: [Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port)

The setup looks like this:

  • docker-compose.yml:

    version: '3'
    
    services:
      apache:
        container_name: apache
        extra_hosts:
          - "host.docker.internal:host-gateway"
        build:
          context: ./local
          dockerfile: ./Apache.dockerfile
        volumes:
          - ./httpdocs:/var/www/html/
          - ./local/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
        ports:
          - "8080:80"
    
  • Apache.Dockerfile:

    FROM php:8.0-apache-bullseye
    
    RUN a2enmod rewrite
    
    RUN docker-php-ext-install -j$(nproc) mysqli
    RUN docker-php-ext-install -j$(nproc) pdo
    RUN docker-php-ext-install -j$(nproc) pdo_mysql
    RUN docker-php-ext-install -j$(nproc) exif
    
    RUN pecl install xdebug && docker-php-ext-enable xdebug
    
  • xdebug.ini:

    zend_extension=xdebug.so
    xdebug.mode=develop,debug
    xdebug.start_with_request=yes
    xdebug.discover_client_host=0
    xdebug.client_host=host.docker.internal
    
  • docker version

    Client: Docker Engine - Community
    Version:           20.10.16
    API version:       1.41
    Go version:        go1.17.10
    Git commit:        aa7e414
    Built:             Thu May 12 09:17:30 2022
    OS/Arch:           linux/amd64
    Context:           default
    Experimental:      true
    
    Server: Docker Engine - Community
    Engine:
      Version:          20.10.16
      API version:      1.41 (minimum version 1.12)
      Go version:       go1.17.10
      Git commit:       f756502
      Built:            Thu May 12 09:15:33 2022
      OS/Arch:          linux/amd64
      Experimental:     false
    containerd:
      Version:          1.6.4
      GitCommit:        212e8b6fa2f44b9c21b2798135fc6fb7c53efc16
    runc:
      Version:          1.1.1
      GitCommit:        v1.1.1-0-g52de29d
    docker-init:
      Version:          0.19.0
      GitCommit:        de40ad0  
    
  • Host: Linux tuxmobile02 5.15.0-33-generic #34-Ubuntu SMP Wed May 18 13:34:26 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

  • IntelliJ 2022.1, Settings > Languages & Frameworks > PHP > Debug

    • Xdebug port: 9003
    • [x] Can accept external connections
  • UFW is disabled:

    $ ufw status
    Status inactive
    

Xdebug client (part of IntelliJ) is running and reachable from the host:

$ nmap -p 9003 localhost
Starting Nmap 7.80 ( https://nmap.org ) at 2022-05-27 10:32 CEST
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000049s latency).

PORT     STATE SERVICE
9003/tcp open  unknown

Nmap done: 1 IP address (1 host up) scanned in 0.03 seconds

But it's not reachable from the docker container:

$ docker exec -it apache /bin/bash
root@cf85dafe5c66:/var/www/html# nmap -p 9003 localhost
Starting Nmap 7.80 ( https://nmap.org ) at 2022-05-27 08:33 UTC
Nmap scan report for localhost (127.0.0.1)
Host is up (0.000041s latency).
Other addresses for localhost (not scanned): ::1

PORT     STATE  SERVICE
9003/tcp closed unknown

Nmap done: 1 IP address (1 host up) scanned in 0.07 seconds

The file /etc/hosts of the docker image looks as expected:

# cat /etc/hosts
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.1  host.docker.internal
172.19.0.3  cf85dafe5c66

=> Any idea what may cause the blocked connection from the docker image to the host?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文