使用 Xdebug 作为调试器在 Docker 容器上通过 VSCode 调试 Laravel

tce*_*tin 6 xdebug docker laravel-5 visual-studio-code

我正在尝试使用 Xdebug 在 Visual Studio Code 上调试 Laravel 代码。我正在使用 Docker 容器作为服务器。

但我在尝试调试时收到此错误:

无法连接到运行时进程,10000 毫秒后超时 - (原因:无法连接到目标:连接 ECONNREFUSED 192.168.99.100:9000)。

这是我的 VSCodelaunch.json

    "version": "0.2.0",
    "configurations": [

        {
            "type": "node",
            "request": "attach",
            "name": "Docker: Attach to Node",
            "port": 9000,
            "address": "192.168.99.100",
            "localRoot": "${workspaceFolder}",
            "remoteRoot": "/var/www",
            "protocol": "inspector",
            "restart": true
        }
]
Run Code Online (Sandbox Code Playgroud)

这是docker-compose.yml

  app:
    build:
      context: ./
      dockerfile: .docker/app.dockerfile
      args:
      - INSTALL_XDEBUG=true
    working_dir: /var/www
    volumes:
      - ./:/var/www
    environment:
      - "DB_PORT=33061"
      - "DB_HOST=192.168.99.100"
      - XDEBUG_CONFIG=remote_host=192.168.99.100 remote_port=9000 remote_connect_back=0
Run Code Online (Sandbox Code Playgroud)

这是我的app.dockerfile

FROM php:7.1-fpm

RUN docker-php-ext-install pdo &&  docker-php-ext-install pdo_mysql

#####################################
# xDebug:
#####################################

ARG INSTALL_XDEBUG=true
RUN if [ ${INSTALL_XDEBUG} = true ]; then \
    # Install the xdebug extension
    pecl install xdebug && \
    docker-php-ext-enable xdebug \
;fi

# Copy xdebug configration for remote debugging
COPY .docker/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini


#####################################
# ZipArchive:
#####################################

ARG INSTALL_ZIP_ARCHIVE=false
RUN if [ ${INSTALL_ZIP_ARCHIVE} = true ]; then \
    # Install the zip extension
    docker-php-ext-install zip \
;fi

# RUN apt-get update && apt-get install -y \
#         freetds-bin \
#         freetds-dev \
#         freetds-common \
#         libct4 \
#     && docker-php-ext-install pdo_dblib

# pdo_dblib
RUN apt-get update && apt-get install -y freetds-dev
RUN docker-php-ext-configure pdo_dblib --with-libdir=/lib/x86_64-linux-gnu
RUN docker-php-ext-install pdo_dblib
RUN sed -i "s|\[sqlsrv\]|\[sqlsrv\]\nhost = 172.18.250.57\nport =1435\nclient charset=UTF-8\ntds version = 8.0|g" /etc/freetds/freetds.conf




RUN usermod -u 1000 www-data

WORKDIR /var/www

CMD ["php-fpm"]

EXPOSE 9000
EXPOSE 9001
Run Code Online (Sandbox Code Playgroud)

我认为 VSCode 无法使用 9000 端口连接到远程 Xdebug。但是当检查应用程序 docker 映像时,Xdebug 工作正常。也许 VSCode 需要更多配置。但我无法解决这个问题。

Anu*_*aut 5

您可以使用PHP Debug VS Code 扩展来连接 XDebug。PHP-FPM 默认在端口 9000 运行,因此最好将设置更改xdebug.remote_port为另一个端口(例如9001):

// launch.json

"version": "0.2.0",
"configurations": [
  {
    "name": "Listen for XDebug",
    "type": "php",
    "request": "launch",
    "pathMappings": { "/": "${workspaceRoot}/" },
    "port": 9001
  },
  {
    "name": "Launch currently open script",
    "type": "php",
    "request": "launch",
    "program": "${file}",
    "cwd": "${fileDirname}",
    "port": 9001
  }
]
Run Code Online (Sandbox Code Playgroud)

如果您使用 Docker,请在php.ini文件中使用以下设置:

//php.ini

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9001
Run Code Online (Sandbox Code Playgroud)

从 Docker 版本 18.03 开始​​,host.docker.internal指向Mac / Windows上的主机 IP 地址。对于 Linux,您可以使用此解决方法

设置好这些设置后,您就可以开始了。