Skip to content

Simplify handling of XDEBUG_CLIENT_HOST #403

Description

@homersimpsons

Previously, we had to manually fetch the host ip from the container. This is rather hacky, and this starts failing for me locally. We should instead default to xdebug://gateway, https://xdebug.org/docs/step_debug#client_host:

xdebug://gateway

Use the system defined network gateway.

Supported on Linux only.

References:

if [ -z "$XDEBUG_CLIENT_HOST" ]; then
export XDEBUG_CLIENT_HOST=`/sbin/ip route|awk '/default/ { print $3 }'`
set +e
# On Windows and MacOS with Docker >= 18.03, check that host.docker.internal exists. it true, use this.
# Linux systems can report the value exists, but it is bound to localhost. In this case, ignore.
host -t A host.docker.internal &> /dev/null
if [[ $? == 0 ]]; then
# The host exists.
DOCKER_HOST_INTERNAL=`host -t A host.docker.internal | awk '/has address/ { print $4 }'`
if [ "$DOCKER_HOST_INTERNAL" != "127.0.0.1" ]; then
export XDEBUG_CLIENT_HOST=$DOCKER_HOST_INTERNAL
export REMOTE_HOST_FOUND=1
fi
fi
if [[ "$REMOTE_HOST_FOUND" != "1" ]]; then
# On mac with Docker < 18.03, check that docker.for.mac.localhost exists. it true, use this.
# Linux systems can report the value exists, but it is bound to localhost. In this case, ignore.
host -t A docker.for.mac.localhost &> /dev/null
if [[ $? == 0 ]]; then
# The host exists.
DOCKER_FOR_MAC_REMOTE_HOST=`host -t A docker.for.mac.localhost | awk '/has address/ { print $4 }'`
if [ "$DOCKER_FOR_MAC_REMOTE_HOST" != "127.0.0.1" ]; then
export XDEBUG_CLIENT_HOST=$DOCKER_FOR_MAC_REMOTE_HOST
fi
fi
fi
set -e
fi

If you enable XDebug, the image will do its best to configure the `xdebug.client_host` to point back to your Docker host.

If you enable XDebug, the image will do its best to configure the `xdebug.client_host` to point back to your Docker host.

Maybe we should adapt the test too

#!/usr/bin/env bash
. ./config
############################################################
## xdebug
############################################################
test_config() {
# Let's check that the "xdebug.client_host" contains a value different from "no value"
docker run ${RUN_OPTIONS} --rm -e PHP_EXTENSION_XDEBUG=1 "${REPO}:${TAG_PREFIX}${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" \
php -i | grep xdebug.client_host | grep -v -q "no value"
assert_equals "0" "$?" '"xdebug.client_host" contains "no value"'
# Let's check that "xdebug.mode" is set to "debug" by default
docker run ${RUN_OPTIONS} --rm -e PHP_EXTENSION_XDEBUG=1 "${REPO}:${TAG_PREFIX}${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" \
php -i | grep xdebug.mode | grep -q "debug"
assert_equals "0" "$?" '"xdebug.mode" is not set to "debug" by default'
# Let's check that "xdebug.mode" is properly overridden
docker run ${RUN_OPTIONS} --rm -e PHP_EXTENSION_XDEBUG=1 -e PHP_INI_XDEBUG__MODE=debug,coverage \
"${REPO}:${TAG_PREFIX}${PHP_VERSION}-${BRANCH}-${BRANCH_VARIANT}" \
php -i | grep xdebug.mode | grep -q "debug,coverage"
assert_equals "0" "$?" '"xdebug.mode" is not properly overridden'
}

Activity

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

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions