【问题标题】:xdebug 3 not working in ubuntu 20.04 with dockerxdebug 3 在 ubuntu 20.04 中无法使用 docker
【发布时间】:2021-01-26 02:33:18
【问题描述】:

我正在尝试在 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

我的 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

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

【问题讨论】:

  • 在您的 docker-compose 文件中,您映射了错误的端口。应该是- "9003:9003"

标签: php docker ubuntu docker-compose vscode-debugger


【解决方案1】:

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

<?php
    phpinfo();

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

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

在您的 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
 

在你的 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" 

现在你可以重建 docker box

docker stop  
docker build --no-cache
docker up

如果您启用了设置,请检查 phpinfo。

如果您对 docker box 有 shell 访问权限,则可以查看日志文件。

tail -n 100 /var/www/log/xdebug.log

如果你运行 PHP Storm,在 PHP Storm >> File >> Settings >> Languages & Frameworks >> PHP >> Debug 中有一个优秀的验证器 点击链接“验证”。将本地 Web 服务器路径设置为您的公用文件夹。验证脚本到您的 Web 地址。在共享文件夹上效果很好,在同步文件夹上效果不佳。

如果您将 Ubuntu 作为主机运行,请查看防火墙设置。 "9000/tcp ALLOW IN Anywhere" 和 9003 端口相同。或者尝试暂时禁用防火墙。

我还在 DockerFile 中添加了端口 9003:9003。但尚未测试这是否有任何区别。

希望这些对你有所帮助。

【讨论】:

  • 谢谢。它适用于 Docker 20.10、Ubuntu 20.04、xDebug 3 和 VS Code。 client_host 不是必需的。
猜你喜欢
  • 1970-01-01
  • 2021-04-25
  • 1970-01-01
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 2020-08-29
  • 2021-08-30
  • 2021-08-10
相关资源
最近更新 更多