Skip to content

Instantly share code, notes, and snippets.

@jeremysu0131
Last active January 11, 2022 07:33
Show Gist options
  • Save jeremysu0131/23ab884dd834ce9b6b7bebc3979b891f to your computer and use it in GitHub Desktop.
Save jeremysu0131/23ab884dd834ce9b6b7bebc3979b891f to your computer and use it in GitHub Desktop.
Laravel 8 debug in docker for vscode / phpstorm

Prerequire:

Make sure you have the docker-compose.yml that generate by default

generate docker file

Execute the following script will generate a dockerfile under root directory

php artisan sail:publish

.env: add the following line

SAIL_XDEBUG_MODE=debug

Set off if you don't want to run xdebug.

chooese any version of php you want to debug

For example, I want to use php 8.1 for debug. So open the php.ini from docker/8.1/ and append the following settings:

[XDebug]
zend_extension = xdebug.so
xdebug.start_with_request = yes
xdebug.discover_client_host = true
xdebug.idekey = VSC
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003

Run the laravel container

./vendor/bin/sail up -d --build

For vscode

  1. Install the extension of felixfbecker.php-debug
  2. Copy the launch.json to .vscode/ to config the debug extesion
  3. Set some break point and press F5 to start debuggin

PhpStorm

  1. toolbar > run > Start listening for PHP debug connections
  2. Open link http://localhost/ and the PHPStorm will pop up a window to let you setup Xdebug. All you need to do is click Accept button. Then setup finished.
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./docker/8.1
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.1/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mariadb
mariadb:
image: 'mariadb:10'
ports:
- '${FORWARD_DB_PORT:-3306}: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:
- 'sailmariadb:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailmariadb:
driver: local
{
// 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 on Docker App",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html": "${workspaceFolder}"
},
"hostname": "localhost",
"xdebugSettings": {
"max_data": 65535,
"show_hidden": 1,
"max_children": 100,
"max_depth": 5
}
},
]
}
[PHP]
post_max_size = 100M
upload_max_filesize = 100M
variables_order = EGPCS
[XDebug]
zend_extension = xdebug.so
xdebug.start_with_request = yes
xdebug.discover_client_host = true
xdebug.idekey = VSC
xdebug.client_host = host.docker.internal
xdebug.client_port = 9003
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment