Table of Contents

PHP - (Debug|Debugger) with Xdebug in Docker

About

How to debug a PHP application running in a docker container.

This articles is split in two:

Environment

Prerequisites

Idea Firewall Public Allow

Configuration

Docker

cd YourApplication
docker run \
    --name myapp \
    -d \
    -p 80:80 \
    -v $(pwd):/var/www/html \
    gerardnico/php-dev:7.4
cd YourApplication
docker run ^
    --name myapp ^
    -d ^
    -p 80:80 ^
    -v %cd%:/var/www/html ^
    gerardnico/php-dev:7.4

Host IP

The docker host IP (ie the IP of the laptop seen from Docker) is hard coded in the docker image.

This IP should match with yours.

The IP in your docker image can be found in the php.ini

# connect to the docker container
docker exec -it doku bash
# cat the php.ini
cat /usr/local/etc/php/php.ini | grep -i xdebug.remote_host
xdebug.remote_host=host.docker.internal
xdebug.remote_host=host.docker.internal

host.docker.internal is the default name given in a docker windows. If this property does not math your environment, you should change it.

How to find the IP can be seen here: docker host IP

Example if you use your own subnet such as below, the IP is something like 10.0.75.1

Docker Network Settings

If you want to get more information on the configuration of Xdebug. See How to install Php Xdebug ?

IDE Client Start listening

The IDE by default listen to the port 9000 but you can change it.

Example Intellij:

Intellij Xdebug Settings

Intellij Start Listening Php Debug Connection

Idea Listening Client Xdebug 9000

Verify the connection from the container

docker exec -it myApp bash
cat /usr/local/etc/php/php.ini | grep -i xdebug
xdebug.remote_port=9000 # client_port in version 3
xdebug.remote_host=host.docker.internal # client_host in version3

apt-get install -y nmap
# scan the port 9000 (xdebug version 2) or 9003 (xdebug version 3)
nmap -Pn -p T:9000 host.docker.internal
# or when nat
# nmap -Pn -p T:9000 10.0.75.1
Starting Nmap 7.40 ( https://nmap.org ) at 2018-06-10 19:58 UTC
Nmap scan report for host.docker.internal (192.168.65.2)
# NAT: Nmap scan report for 10.0.75.1
Host is up (0.0012s latency).
PORT     STATE SERVICE
9000/tcp open  cslistener

Nmap done: 1 IP address (1 host up) scanned in 0.37 seconds

Browser

In order to activate the debugger, a special GET/POST or COOKIE parameter must be set.

It can be easily done through a browser plugin:

Start Debug

Now that the whole system is set up, we can start a debug session with the help of the following steps.

I use and you can see the dokuwiki application on the snapshot.

Intellij Start Listening Php Debug Connection

Firefox Easy Xdebug Enable

Intellij Incoming Xdebug Connection

Intellij Map Server Path Local Path

Intellij Debugger Variable Stack

Annexe

Support

Cannot accept external Xdebug connection: Cannot evaluate expression 'isset(_SERVER['PHP_IDE_CONFIG'])'

You may see this error when an XDebug Session start.

Cannot accept external Xdebug connection: Cannot evaluate expression 'isset($_SERVER['PHP_IDE_CONFIG'])'

Cannot Evaluate Php Ide Config

The environment variable PHP_IDE_CONFIG is used to define the debug server name. See Configuring path mappings in Zero Configuration Debuggin

This is because as the incoming connection doesn't match any server in your server definition, the IDE will try to read this variable.

Solution:

Documentation / Reference