Skip to content

Instantly share code, notes, and snippets.

@fideloper
Last active August 31, 2021 10:59
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fideloper/d3d0d0add6d37bec050a8277e51da459 to your computer and use it in GitHub Desktop.
Save fideloper/d3d0d0add6d37bec050a8277e51da459 to your computer and use it in GitHub Desktop.
Enable/Disable xDebug depending on env.
#!/usr/bin/env bash
###
# A CMD or ENTRYPOINT script for a Dockerfile to use to start a Nginx/PHP-FPM
#
# For more details, see 🐳 https://shippingdocker.com
##
if [ ! "production" == "$APP_ENV" ] && [ ! "prod" == "$APP_ENV" ]; then
# Enable xdebug
## FPM
ln -sf /etc/php/7.0/mods-available/xdebug.ini /etc/php/7.0/fpm/conf.d/20-xdebug.ini
## CLI
ln -sf /etc/php/7.0/mods-available/xdebug.ini /etc/php/7.0/cli/conf.d/20-xdebug.ini
else
# Disable xdebug
## FPM
if [ -e /etc/php/7.0/fpm/conf.d/20-xdebug.ini ]; then
rm -f /etc/php/7.0/fpm/conf.d/20-xdebug.ini
fi
## CLI
if [ -e /etc/php/7.0/cli/conf.d/20-xdebug.ini ]; then
rm -f /etc/php/7.0/cli/conf.d/20-xdebug.ini
fi
fi
# Config /etc/php/7.0/mods-available/xdebug.ini
# to correctly set the remote_host to your host computer's IP address
sed -i "s/xdebug\.remote_host\=.*/xdebug\.remote_host\=$XDEBUG_HOST/g" /etc/php/7.0/mods-available/xdebug.ini
# start supervisord, which
# in turn will start Nginx/PHP-FPM
/usr/bin/supervisord
zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.idekey=docker
; This IP address will get changed
; dynamically when the container starts
xdebug.remote_host=192.168.1.2
@fideloper
Copy link
Author

Requires the following environment variables:

  • APP_ENV - e.g. "local", "production", "dev"
  • XDEBUG_HOST - an IP of your host machine, accessible by a container. On a Mac, obtain this like so:
export XDEBUG_HOST=$(ipconfig getifaddr en1)

@cnaslain
Copy link

You need to run your container as root to be allowed to create these sym links... am I correct?
If so, this doesn't match Docker' security best-practices. Is there an alternative solution using maybe confd to set this?

@ssi-anik
Copy link

if someone wants the XDEBUG_HOST on your Linux machine, try this out to get the host address: https://medium.com/@sirajul.anik/docker-for-linux-localhost-docker-connect-to-host-machine-from-a-docker-container-in-linux-fa42b00f161e

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