Skip to content

Instantly share code, notes, and snippets.

@YSaxon
Last active October 25, 2023 18:01
Show Gist options
  • Save YSaxon/330cd6baf22ccbec5cb86086a1094b9c to your computer and use it in GitHub Desktop.
Save YSaxon/330cd6baf22ccbec5cb86086a1094b9c to your computer and use it in GitHub Desktop.
php debugging in docker container

Installing xdebug

Manually

apt install php7.4-debug

nano etc/php/7.4/apache2/php.ini

[Xdebug]
xdebug.mode = debug
xdebug.client_host = host.docker.internal
xdebug.client_port = 29003 ; default is 9003 but I had a conflict with that port
xdebug.start_with_request = yes
zend_extension=xdebug.so
xdebug.mode=debug

/etc/init.d/apache2 restart

Or adding it to your Dockerfile

Alternatively, you could add this to near the end of your Dockerfile:

# Install Xdebug
RUN PHP_VERSION=$(php -r "echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION;") && \
    apt-get install -y "php${PHP_VERSION}-xdebug"

# Append Xdebug settings to php.ini
RUN PHP_INI_DIR=$(php -i | grep php.ini$ | rev | cut -d ' ' -f 1 | rev) && \
    { \
      echo '[Xdebug]'; \
      echo 'xdebug.mode = debug'; \
      echo 'xdebug.client_host = host.docker.internal'; \
      echo 'xdebug.client_port = 29003'; \
      echo 'xdebug.start_with_request = yes'; \
      echo 'zend_extension=xdebug.so'; \
    } >> "$PHP_INI_DIR"

# Restart Apache to apply changes
RUN /etc/init.d/apache2 restart

port forwarding:

Docker Compose

Add to your docker-compose.yml

services:
  php:
    ports:
      - 29003:29003

VSCODE

In VSCode: .vscode/launch.json

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 29003,
            "pathMappings": {
               // you'll figure out the mappings when it first doesn't work
               // could be something like
               // "/var/www/html": "${workspaceRoot}/src"
            }
        }
    ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment