Skip to content

Instantly share code, notes, and snippets.

@akerouanton
Last active October 20, 2020 09:18
Show Gist options
  • Save akerouanton/8d83ccd28ed1b5e23e702ded0af755ea to your computer and use it in GitHub Desktop.
Save akerouanton/8d83ccd28ed1b5e23e702ded0af755ea to your computer and use it in GitHub Desktop.
A little script to easily start a debug session on PHP CLI scripts

To use this script to start a debug session on a CLI script: php-debug vendor/bin/behat features/attribution.feature:555.

FROM php:7-fpm-alpine AS fpm-prod
# ...
#############################
FROM fpm-prod AS fpm-dev
USER 0
# net-tools is used by the php-debug script to get the IP address of the host
# system.
RUN apk add --no-cache net-tools && \
pecl install xdebug && \
docker-php-ext-enable xdebug && \
rm -rf /tmp/pear
COPY docker/php-debug /usr/local/bin/php-debug
USER 1000
FROM php:7-fpm-buster AS fpm-prod
# ...
#############################
FROM fpm-prod AS fpm-dev
USER 0
# net-tools is used by the php-debug script to get the IP address of the host
# system.
RUN apt-get update && \
apt-get install -y --no-install-recommends net-tools && \
pecl install xdebug && \
docker-php-ext-enable xdebug && \
rm -rf /var/lib/apt/lists/* /tmp/pear
COPY docker/php-debug /usr/local/bin/php-debug
USER 1000
#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail
[[ "${DEBUG:-}" != "" ]] && set -o xtrace
# Find the default gateway (it points to the host system on Linux and probably
# on WSL2, but probably not on MacOS).
source_ip=$(route | grep default | awk '{print $2}')
# PHP_IDE_CONFIG is used by PHPStorm (and presumably other IDEs) to know which
# PHP server is triggering a debug session and thus to known which paths mapping
# it should use. A default value is defined here but this could be customized
# through the .env file at the root of the project.
export PHP_IDE_CONFIG=${PHP_IDE_CONFIG:-serverName=cadi-fpm}
php -d display_errors=on \
-d xdebug.remote_autostart=on \
-d "xdebug.remote_host=${source_ip}" \
${@}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment