使用Xdebug作为调试器在Docker容器上使用VSCode调试Laravel [英] Debugging Laravel with VSCode on Docker container using Xdebug as debugger
问题描述
我正在尝试使用Xdebug在Visual Studio Code上调试Laravel代码.我正在使用充当服务器的Docker容器.
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:
无法连接到运行时进程,在10000毫秒后超时-(原因:无法连接到目标:连接ECONNREFUSED 192.168.99.100:9000).
Cannot connect to runtime process, timeout after 10000 ms - (reason: Cannot connect to the target: connect ECONNREFUSED 192.168.99.100:9000).
这是我的VSCode launch.json
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
}
]
这里是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
这是我的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
我认为VSCode无法通过9000端口连接到远程Xdebug.但是当检查应用程序docker image时Xdebug正常工作.也许VSCode需要更多配置.但是我无法解决这个问题.
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.
推荐答案
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
}
]
如果使用的是Docker,请在php.ini
文件中使用以下设置:
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
从Docker 18.03版开始,host.docker.internal
指向 Mac上的主机IP地址. / Windows .对于Linux,您可以使用解决方法.
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.
这篇关于使用Xdebug作为调试器在Docker容器上使用VSCode调试Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!