條件準(zhǔn)備:在php的dockerfile中已安裝xdebug3,nginx是已有鏡像。
docker-compose 配置路徑如下
├── Dockerfile
├── docker-compose.yml
├── nginx-configs
│ ├── conf.d
│ │ ├── test.inner.conf
│ └── nginx.conf
└── php-configs
└── docker-php-ext-xdebug.ini
運(yùn)行php環(huán)境:docker-compose up -d
docker-compose.yml 配置文件
version: '3'
services:
php:
build: .
container_name: php-dev
restart: always
networks:
- dev-network
ports:
- "9000:9000"
volumes:
- /Users/qinyongbo/code/qn:/data/code/qn
- ./php-configs/docker-php-ext-xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
command:
- php-fpm
nginx:
container_name: nginx-dev
image: "registry.cn-shenzhen.aliyuncs.com/image/nginx:1.17.9-test-service-20201123"
restart: always
networks:
- dev-network
ports:
- "80:80"
volumes:
- /Users/qinyongbo/code/qn:/data/code/qn
- ./nginx-configs/nginx.conf:/etc/nginx/nginx.conf
- ./nginx-configs/conf.d:/etc/nginx/conf.d
networks:
dev-network:
external: true
docker-php-ext-xdebug.ini
[xdebug]
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
#本機(jī)ip,因?yàn)閐ocker在mac中不能使用host網(wǎng)絡(luò)模式,故這里不能用localhost
xdebug.client_host=192.168.10.89
#本機(jī)端口
xdebug.client_port=9003
xdebug.idekey = PHPSTORM
以上鏡像相關(guān)的已準(zhǔn)備完成,接下來是PhpStorm配置,選擇Languages & Frameworks》PHP

image.png
如果沒有CLI interpreter ,那么就新建一個(gè),點(diǎn)擊左上角+,選擇Docker

image.png
在Server中點(diǎn)擊New一個(gè)

image.png
新建Server過程中,Path mappings配置需要選擇docker中路徑和本機(jī)路徑匹配。

image.png
點(diǎn)擊Configuration File(s),選擇docker-compose.yml配置文件,service選擇對應(yīng)的php服務(wù)名字。
到此CLI interpreter 添加成功。
添加Servers

image.png
開啟xdebug 監(jiān)聽,默認(rèn)監(jiān)聽端口為9003

image.png
測試curl http://test.example.com/Index/index,請求一直loading,代碼進(jìn)入調(diào)試模式

image.png
以上http請求模式完成,那CLI模式怎么弄呢?
在docker中執(zhí)行
export XDEBUG_CONFIG="xdebug.mode=debug xdebug.start_with_request=yes xdebug.client_host=192.168.2.159 xdebug.client_port=9003 xdebug.idekey = PHPSTORM"
export PHP_IDE_CONFIG="serverName=DomainName"
執(zhí)行:php path/index.php,就發(fā)現(xiàn)IDE中有debug在運(yùn)行。