在laravel帆dockerWindowsPhpStorm上运行Xdebug
因此,经过数小时的谷歌搜索,没有找到正确的答案。我已经开始使用 Docker 和 Laravel 的风帆为 Windows 安装 Laravel 8.x 教程。
现在我想使用 Xdebug 并且完全不知道该怎么做。
在项目的根目录中有一个 docker-compose.yml (这是默认值)
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
PHP_IDE_CONFIG: 'serverName=localhost'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
# - selenium
# selenium:
# image: 'selenium/standalone-chrome'
# volumes:
# - '/dev/shm:/dev/shm'
# networks:
# - sail
# depends_on:
# - laravel.test
mysql:
image: 'mysql:8.0'
ports:
- '${DB_PORT}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
redis:
image: 'redis:alpine'
ports:
- '${REDIS_PORT}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
# memcached:
# image: 'memcached:alpine'
# ports:
# - '11211:11211'
# networks:
# - sail
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- 1025:1025
- 8025:8025
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
在 vendor/laravel/sail/runtimes/8.0 文件夹中有一个 Dockerfile
FROM ubuntu:20.04
LABEL maintainer="Taylor Otwell"
ARG WWWGROUP
WORKDIR /var/www/html
ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update
&& apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin
&& mkdir -p ~/.gnupg
&& echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf
&& apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E5267A6C
&& apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C300EE8C
&& echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list
&& apt-get update
&& apt-get install -y php8.0-cli php8.0-dev
php8.0-pgsql php8.0-sqlite3 php8.0-gd
php8.0-curl php8.0-memcached
php8.0-imap php8.0-mysql php8.0-mbstring
php8.0-xml php8.0-zip php8.0-bcmath php8.0-soap
php8.0-intl php8.0-readline
php8.0-msgpack php8.0-igbinary php8.0-ldap
php8.0-redis
&& php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
&& curl -sL https://deb.nodesource.com/setup_15.x | bash -
&& apt-get install -y nodejs
&& apt-get -y autoremove
&& apt-get clean
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.0
RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail
COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/8.0/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container
EXPOSE 8000
ENTRYPOINT ["start-container"]
怎么办?我在 Windows 系统上,我想将它连接到 PhpStorm
回答
我在 Linux 工作站上的 VS Code 遇到了同样的问题。
也许我的解决方案也适用于您。
显然,Laravel Sail 根本没有安装 XDebug。为了包含它,您必须修改Dockerfile、编辑docker-compose.yml和重建容器。
这就是我所做的。
- 我在一个更方便的地方复制了 Laravel Sail 使用的 Docker 配置:
cp -r vendor/laravel/sail/runtimes/8.0 ./resources/docker/
- 我更改了
context并args在以下的第一行添加了一个变量docker-compose.yml:
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./resources/docker/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
XDEBUG: ${APP_DEBUG}
...
所以context我复制了原始 Docker 配置的点。我还决定将新的XDEBUGarg绑定到文件中APP_DEBUG变量的值.env,以便在生产环境中关闭 XDebug。
- 然后,我更改了
Dockerfile我之前复制的内容,以便在构建容器时包含 xdebug。该脚本还应该在里面为 Xdebug 3 编写正确的配置选项php.ini:
FROM ubuntu:20.04
LABEL maintainer="Taylor Otwell"
ARG WWWGROUP
ARG XDEBUG
WORKDIR /var/www/html
ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update
&& apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin
&& mkdir -p ~/.gnupg
&& echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf
&& apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E5267A6C
&& apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C300EE8C
&& echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list
&& apt-get update
&& apt-get install -y php8.0-cli php8.0-dev
php8.0-pgsql php8.0-sqlite3 php8.0-gd
php8.0-curl php8.0-memcached
php8.0-imap php8.0-mysql php8.0-mbstring
php8.0-xml php8.0-zip php8.0-bcmath php8.0-soap
php8.0-intl php8.0-readline
php8.0-msgpack php8.0-igbinary php8.0-ldap
php8.0-redis
RUN if [ ${XDEBUG} ] ; then
apt-get install -y php-xdebug
&& echo "[XDebug]" > /etc/php/8.0/cli/php.ini
&& echo "zend_extension="$(find /usr/lib/php/20200930/ -name xdebug.so)" > /etc/php/8.0/cli/php.ini"
&& echo "xdebug.mode = debug" >> /etc/php/8.0/cli/php.ini
&& echo "xdebug.start_with_request = yes" >> /etc/php/8.0/cli/php.ini
&& echo "xdebug.discover_client_host = true" >> /etc/php/8.0/cli/php.ini ;
fi;
RUN php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
&& curl -sL https://deb.nodesource.com/setup_15.x | bash -
&& apt-get install -y nodejs
&& apt-get -y autoremove
&& apt-get clean
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.0
RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail
COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/8.0/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container
EXPOSE 8000
ENTRYPOINT ["start-container"]
- 停止、重建和重新启动容器后:
$ ./vendor/bin/sail stop
$ ./vendor/bin/sail up --build -d
您可以查看 XDebug 是否正在运行:
$ ./vendor/bin/sail php -v
PHP 8.0.0 (cli) (built: Nov 27 2020 12:26:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
with Zend OPcache v8.0.0, Copyright (c), by Zend Technologies
with Xdebug v3.0.1, Copyright (c) 2002-2020, by Derick Rethans
仅适用于 VSCode:
-
里面
Preferences -> Settings下Debug你应该检查“调试:无处不在允许断点”。 -
更改默认
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}"
}
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9000
}
]
}
- Rather than copying the original docker files, you should be able just use `php artisan sail:publish` (as per [the documentation](https://laravel.com/docs/8.x/sail#sail-customization))
- it's critical to insert the lines in the Dockerfile in the middle of the existing RUN command (i.e. split it into two before you add the xdebug block). Simply putting the xdebug block after it does NOT work! I spent two hours figuring that out 🙁
- I followed these instructions and can see `sail php -v` includes xdebug...but now what? If I open VSC and run the "Listen for XDebug" (by hitting F5), then go to my terminal and run `sail test`, I get lots of errors saying `Xdebug: [Step Debug] Could not connect to debugging client. Tried: localhost:9003 (fallback through xdebug.client_host/xdebug.client_port) :-(`
-
I figured it out...I added the following lines after the `discover_client_host` line: ```&& echo "xdebug.idekey = VSC" >> /etc/php/8.0/cli/php.ini
&& echo "xdebug.client_host = host.docker.internal" >> /etc/php/8.0/cli/php.ini
&& echo "xdebug.client_port = 9003" >> /etc/php/8.0/cli/php.ini;```
回答
在 laravel/sail(PHP 7.4 和 PHP 8)中有一个废弃的 PR 用于可选的 Xdebug 3.0 支持。请关注讨论。
检查提交,要更改的内容Dockerfile,docker-compose.yml.不要忘记在 .env 中设置变量
回答
在上面海报的一些帮助之后,我让 XDebug 处理网络请求。
我对 PHPStorm 和 XDebug 2.x 有相当多的经验。我是 docker 新手,所以可能有更好的方法来解决这个问题。我还没有弄清楚如何运行或调试依赖于 PHPStorm 内部数据库连接的测试(右键单击测试进行调试)。如果我正在“侦听” sail test(这将正确运行测试),它们会使用断点成功运行,但 PHPStorm 在运行测试时找不到 MySQL 数据库,我也收到此错误:“Xdebug: [Step Debug] 无法连接到调试客户端。试过:host.docker.internal:9003"
更新 1/18/21 以修复 PHPStorm 中的本地环境,以便它可以找到数据库和 Docker 网络的其余部分。现在我可以在 PHPStorm 中成功运行测试和调试。对于 PHPStorm 2020.3.1 的当前版本,您需要在要求网络模式的位置添加网络名称。我将向他们报告此事,以便尽快解决。
通过运行获取您的网络名称docker network ls。在这种情况下,它是 myProjectName_sail。
NETWORK ID NAME DRIVER SCOPE
8e8635ce01a6 bridge bridge local
401307dbfaad host host local
ad8020ad629e myProjectName_sail bridge local
d85a9668cade none null local
在 PHPStorm Preferences>PHP>CLI Interpreter>...
关于调试 Web 请求的修复:
我拥有的:
- Laravel Sail 8.0 版
- Xdebug 版本 3.0
- PHPStorm 版本 2020.3 PHPStorm 的版本很重要,因为它支持 PHP8 和 Xdedbug 3。
我做了什么:
- 我通过将这一行添加到 Dockerfile 来在我的 docker 容器中安装了 vim:
RUN apt-get -y install vim - 这让我可以通过运行
docker exec -it mySite.test_1 vim /etc/php/8.0/cli/php.ini. - 在这种情况下,我仍然编辑 Dockerfile 以生成 php.ini
你需要让你的/etc/php/8.0/cli/php.ini文件看起来像这样(client_host 是关键):
[XDebug]
xdebug.mode = debug
xdebug.start_with_request = yes
xdebug.client_host = host.docker.internal
我按照@Enea74 的建议将其添加到 Dockerfile 中。我很难让条件返回 TRUE,所以我TRUE现在在这里硬编码:
RUN if [ true ] ; then
apt-get install -y php-xdebug
&& echo "[XDebug]" > /etc/php/8.0/cli/php.ini
&& echo "zend_extension="$(find /usr/lib/php/20200930/ -name xdebug.so)" > /etc/php/8.0/cli/php.ini"
&& echo "xdebug.mode = debug" >> /etc/php/8.0/cli/php.ini
&& echo "xdebug.start_with_request = yes" >> /etc/php/8.0/cli/php.ini
&& echo "xdebug.client_host = host.docker.internal" >> /etc/php/8.0/cli/php.ini ;
fi;
- 我通过运行构建了 Docker 容器
sail build --no-cache - 然后
sail up -d
sail php -v
返回:
PHP 8.0.0 (cli) (built: Nov 27 2020 12:26:22) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.0-dev, Copyright (c) Zend Technologies
with Zend OPcache v8.0.0, Copyright (c), by Zend Technologies
with Xdebug v3.0.1, Copyright (c) 2002-2020, by Derick Rethans
现在在 PHPStorm 中执行以下操作。我认为它自己发现了许多这些设置:我不记得设置所有这些值:
希望这会帮助某人。