php 使用VSCode和Docker调试Yii 1.1

j9per5c4  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(139)

我想问一下关于调试Yii 1.1应用程序的问题。我已经试着在StackOverflow和其他网站上实现了答案,但是我的VSCode仍然无法调试应用程序,已经设置的断点根本无法读取。我正在使用Docker运行Yii。
下面是我使用的文件的详细信息。

停靠-撰写.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调试器,但结果是:

任何帮助都是非常感谢的。
是否缺少任何配置?

i7uq4tfw

i7uq4tfw1#

在观看了几个关于如何为在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一样工作

相关问题