Skip to content

Instantly share code, notes, and snippets.

@carstenwindler
Last active February 6, 2024 08:20
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save carstenwindler/b330028c41c453c905cbe0801470afd2 to your computer and use it in GitHub Desktop.
Save carstenwindler/b330028c41c453c905cbe0801470afd2 to your computer and use it in GitHub Desktop.
Bash script to quickly enable / disable xdebug in a PHP docker container
#!/usr/bin/env bash
# see https://carstenwindler.de/php/enable-xdebug-on-demand-in-your-local-docker-environment/
if [ "$#" -ne 1 ]; then
SCRIPT_PATH=`basename "$0"`
echo "Usage: $SCRIPT_PATH enable|disable"
exit 1;
fi
# Expects service to be called app in docker-compose.yml
SERVICE_ID=$(docker-compose ps -q app)
if [ "$1" == "enable" ]; then
docker exec -i $SERVICE_ID bash -c \
'[ -f /usr/local/etc/php/disabled/docker-php-ext-xdebug.ini ] && cd /usr/local/etc/php/ && mv disabled/docker-php-ext-xdebug.ini conf.d/ || echo "Xdebug already enabled"'
else
docker exec -i $SERVICE_ID bash -c \
'[ -f /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini ] && cd /usr/local/etc/php/ && mkdir -p disabled/ && mv conf.d/docker-php-ext-xdebug.ini disabled/ || echo "Xdebug already disabled"'
fi
docker restart $SERVICE_ID
docker exec -i $SERVICE_ID bash -c '$(php -m | grep -q Xdebug) && echo "Status: Xdebug ENABLED" || echo "Status: Xdebug DISABLED"'
@rvanbaalen
Copy link

You're a life saver! Went from 15+ minutes to run 300 tests to 2 minutes by disabling xdebug in my docker container using this script.

@carstenwindler
Copy link
Author

I'm happy to hear that it helped you @rvanbaalen!

@Apen
Copy link

Apen commented Dec 16, 2021

Thanks a lot ! Simple and working :-)

@carstenwindler
Copy link
Author

Thanks @Apen :-)

@gigabites19
Copy link

Thank you, works very well with minimal adjustments needed.

@carstenwindler
Copy link
Author

I'm glad it helped you @gigamarr ! Thanks for giving your feedback, I really appreciate that 👍

@krisztian-fekete
Copy link

Running xdebug.sh disable I get

Xdebug already disabled
91a746fb170266e43bfb0d924edaeea3bbcd896fb12c4831c711b0ad1b6bd224
Status: 
Xdebug ENABLED

So is it disabled or enabled ? 😅

@carstenwindler
Copy link
Author

@krisztian-fekete impossible to tell from the amount of data you provided 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment