xdebug 3 无法在 ubuntu 20.04 中使用 docker 运行

Pau*_*CaP 3 php ubuntu docker docker-compose vscode-debugger

我正在尝试在 ubuntu 20.04 上将 xdebug 3 与 docker 一起使用,但我没有成功,xdebug 没有进入中断点,我已经搜索了所有内容,但没有答案解决了我的问题,这可能与 docker 主机有关,因为相同的配置在Windows中是正确的,我不知道我还能尝试什么来解决这个问题,我需要帮助来理解我做错了什么,下面是我的配置。

我的 docker-compose 文件

version: '3.7'

networks:
  supervisao:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: supervisao-web
    ports:
      - "80:80"
    volumes:
      - .:/var/www/html/
      - ./.docker/web/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
      - mysql
    networks:
      - supervisao
  mysql:
    image: mysql:latest
    container_name: supervisao-db
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    tty: true
    ports:
      - "3306:3306"
    volumes:
      - ./.docker/mysql/:/var/lib/mysql
    environment:
      MYSQL_DATABASE: supervisao
      MYSQL_USER: user
      MYSQL_PASSWORD: pass
      MYSQL_ROOT_PASSWORD: pass
      SERVICES_TAGS: dev
      SERVICES_NAME: mysql
    networks:
      - supervisao
  php:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: supervisao-php
    volumes:
      - .:/var/www/html/
      - ./.docker/php/docker-xdebug.ini:/usr/local/etc/php/conf.d/php-docker.ini
    ports:
      - "9000:9000"
    networks:
     - supervisao
  redis:
    image: redis:latest
    volumes:
      - ./.docker/redis:/data
    ports:
      - 6379:6379
    networks:
      - supervisao
Run Code Online (Sandbox Code Playgroud)

我的 xdebug.ini

# File: docker-xdebug.ini
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so
xdebug.discover_client_host=1
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003
xdebug.log = /var/www/html/xdebug.log
Run Code Online (Sandbox Code Playgroud)

如果有人可以合作,我很感激,谢谢

小智 5

确保 PHP 中已加载 xdebug 3。创建 phpinfo.php 文件

<?php
    phpinfo();
Run Code Online (Sandbox Code Playgroud)

浏览它以检查 xdebug 是否由 PHP 加载并且是版本 3。

如果 xdebug 是版本 2.x,那么您可以使用 pecl 安装 v3。如果您具有对 docker 盒子的 shell 访问权限,您可以直接尝试 pecl 命令,并在 phpinfo 中查看是否有 xdebug v3。

在你的 DockerFile 中:

# PECL
RUN mkdir -p /tmp/pear/cache
RUN pecl channel-update pecl.php.net
RUN apt install -y php-pear

COPY xdebug.ini "/etc/php/${PHP_VERSION}/mods-available/xdebug.ini"
# The xdebug distributed with Ubuntu 20.04 LTS is v2.9.2, we want v3.0.x
RUN pecl install xdebug
# Enable xdebug by default
RUN phpenmod xdebug
 
Run Code Online (Sandbox Code Playgroud)

在你的 xdebug.ini 中

zend_extension=xdebug.so
xdebug.default_enable = On
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.discover_client_host=yes
xdebug.max_nesting_level = -1
xdebug.log = "/var/www/log/xdebug.log"
xdebug.output_dir = "/var/www/log/profiler" 
Run Code Online (Sandbox Code Playgroud)

现在你可以重建 docker 盒子了

docker stop  
docker build --no-cache
docker up
Run Code Online (Sandbox Code Playgroud)

检查 phpinfo 是否启用了设置。

如果您可以通过 shell 访问 docker 盒子,则可以查看日志文件。

tail -n 100 /var/www/log/xdebug.log
Run Code Online (Sandbox Code Playgroud)

如果您运行 PHP Storm,则 PHP Storm >> 文件 >> 设置 >> 语言和框架 >> PHP >> 调试中有一个出色的验证器 单击链接“验证”。将本地 Web 服务器路径设置为您的公用文件夹。您的 webb 地址的验证脚本。在共享文件夹上效果很好,但在同步文件夹上效果不佳。

如果您将 Ubuntu 作为主机运行,请查看防火墙设置。“9000/tcp ALLOW IN Anywhere”对于端口 9003 也是如此。或者尝试暂时禁用防火墙。

我还将端口 9003:9003 添加到了 DockerFile。但还没有测试过这是否有任何区别。

希望以上内容对您有所帮助。