Debugging Laravel with VSCode on Docker container using Xdebug as debugger

社会主义新天地 提交于 2020-03-23 08:09:32

问题


I am trying to debug Laravel code on Visual Studio Code using Xdebug. I am using Docker container that works as server.

But I am getting this error when try to debug:

Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 192.168.99.100:9000).

Here is my VSCode launch.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
        }
]

Here is 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

Here is my 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

I think VSCode can't connect to remote Xdebug with 9000 port. But when check to app docker image Xdebug is working properly. Maybe VSCode needs some more configuration. But I couldn't solve this issue.


回答1:


You can use PHP Debug VS Code extension to connect with XDebug. PHP-FPM are run at port 9000 by default, so it's best to change the xdebug.remote_port settings to another port (e.g. 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
  }
]

If you are using Docker, Use these settings in your php.ini file :

//php.ini

[XDebug]
xdebug.remote_enable = 1
xdebug.remote_autostart = 1
xdebug.remote_host = host.docker.internal
xdebug.remote_port = 9001

As of Docker version 18.03, host.docker.internal points to host IP address on Mac / Windows. For Linux, you can use this workaround.

Once these settings are set, you are good to go.



来源:https://stackoverflow.com/questions/51175004/debugging-laravel-with-vscode-on-docker-container-using-xdebug-as-debugger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!