【问题标题】:Debug Yii 1.1 using VSCode and Docker使用 VSCode 和 Docker 调试 Yii 1.1
【发布时间】:2022-12-19 18:51:31
【问题描述】:

我想问一下有关调试 Yii 1.1 应用程序的问题。我已经尝试在 StackOverflow 和其他网站上实现答案,但我的 VSCode 仍然无法调试应用程序,根本无法读取已设置的断点。我正在使用 Docker 来运行 Yii。

这是我使用的文件的详细信息。

docker-compose.yml

version: '3'
services:
  web:
    container_name: php72
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www/html
      - ./xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
    networks:
      - app-network

  mysql:
    image: mysql:8.0.31-oracle
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
      MYSQL_USER: 'admin'
      MYSQL_PASSWORD: '123456'
      MYSQL_DATABASE: 'test_db'
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - 3306:3306
    networks:
      - app-network

networks:
  app-network:

volumes:
  db_data:

文件

FROM php:7.2-apache

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions gd xdebug pdo pdo_mysql pdo_pgsql mongodb mbstring zip


EXPOSE 80

xdebug.ini文件

zend_extension=xdebug

[xdebug]
xdebug.mode=debug
xdebug.discover_client_host=1
xdebug.idekey=VSCODE
xdebug.start_with_request=yes
xdebug.client_port=9003
xdebug.remote_host="host.docker.internal"

启动.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 0,
            "runtimeArgs": [
                "-dxdebug.start_with_request=yes"
            ],
            "env": {
                "XDEBUG_MODE": "debug,develop",
                "XDEBUG_CONFIG": "client_port=${port}"
            }
        },
        {
            "name": "Launch Built-in web server",
            "type": "php",
            "request": "launch",
            "runtimeArgs": [
                "-dxdebug.mode=debug",
                "-dxdebug.start_with_request=yes",
                "-S",
                "localhost:8001"
            ],
            "program": "",
            "cwd": "${workspaceRoot}/../../",
            "port": 9003,
            "serverReadyAction": {
                "pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
                "uriFormat": "http://localhost:%s",
                "action": "openExternally"
            }
        }
    ]
}

这是我的项目结构:

我在浏览器中使用 localhost:8000 访问我的应用程序,然后我尝试打开 VSCode 调试器,但这是结果:

很感谢任何形式的帮助。

是否缺少任何配置?

【问题讨论】:

  • 1) 在调用xdebug_info(); 的地方制作一些页面(URL 端点)并通过浏览器调用它(您尝试调试代码的方式)。仔细检查实际的 Xdebug 设置是否符合您的预期(也请在此处显示屏幕截图,以防出现更多不匹配)。另外:youtube.com/watch?v=IN6ihpJSFDw 2) 如果您的代码在 Docker 中,那么最好删除 xdebug.discover_client_host=1 行。
  • 3) 根据你的第二张截图(Xdebug 日志位),Xdebug 首先尝试连接到192.168.208.1:9003(你的自动检测到的 IP)然后回落到xdebug.client_host..这是localhost而不是预期的host.docker.internal。那是因为您使用的是 Xdebug v2 参数名称 xdebug.remote_host 而不是 v3 名称 xdebug.client_host (检查 xdebug.org/docs/upgrade_guide )——这是您可以从#1 中发现的最明显的事情。

标签: php docker yii xdebug vscode-debugger


【解决方案1】:

在观看了几个关于如何为在 docker 容器内运行的应用程序设置 xdebug 的视频后,我终于找到了适合我的案例的答案

我把我的 docker-compose.yml 改成了这样

version: '3'
services:
  web:
    container_name: php72
    build:
      context: .
      dockerfile: Dockerfile
    extra_hosts:
      - "host.docker.internal:host-gateway" // Add extra host for docker
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www/html // this is the remote path where my apps installed
      - ./xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
    networks:
      - app-network

  mysql:
    image: mysql:8.0.31-oracle
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: '123456'
      MYSQL_USER: 'admin'
      MYSQL_PASSWORD: '123456'
      MYSQL_DATABASE: 'wms_test'
    volumes:
      - db_data:/var/lib/mysql
    ports:
      - 3306:3306
    networks:
      - app-network

networks:
  app-network:

volumes:
  db_data:

并且按照@LazyOne 的建议,因为我使用的是 Xdebug 3,所以我也将 xdebug.ini 更改为这样

zend_extension=xdebug

[xdebug]
xdebug.mode=develop,debug
xdebug.start_with_request=yes
xdebug.remote_port=9003
xdebug.client_port=9003
xdebug.client_host=host.docker.internal
xdebug.idekey="VSCODE"
xdebug.log=/tmp/xdebug_remote.log

重要的是,在观看了这个 youtube 视频Setup Xdebug WITH DOCKER and debug in VSCode 之后,我弄清楚了我之前的设置中缺少什么,那就是 pathMapping 所以我将我的 launch.json 更改为这样

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "/var/www/html" : "${workspaceFolder}"
            }
        }
    
    ]
}

瞧,这就像一个 charm

【讨论】:

    猜你喜欢
    • 2019-03-05
    • 2020-03-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    • 2020-09-18
    • 2020-01-15
    • 2019-07-19
    • 1970-01-01
    相关资源
    最近更新 更多