the_lar's avatar

How do I configure xDebug in PHP using Docker containers?

Hi all,

Been getting to grips with Dockerizing my new Laravel app using this series - https://laracasts.com/series/the-docker-tutorial - I've set things up exactly as per this and all works great!

I would now like to install xDebug in order to debug using my IDE which is PHPStorm and I'm not really sure what I need to do. I did find this thread from 3 years ago - https://laracasts.com/discuss/channels/general-discussion/docker-phpstorm-xdebug but there were no replies. Trying out some of the things the OP did doesn't seem to work either.

So I'm looking for any help with what I actually put in my php.dockerfile AND whether I need any specific config in PHPStorm for it to work?

Much appreciated Kevin

0 likes
5 replies
the_lar's avatar

Just as a starting point, I HAVE managed to install xDebug with this in my php.dockerfile:

FROM php:8-fpm-alpine
ENV PHPGROUP=laravel
ENV PHPUSER=laravel
RUN adduser -g ${PHPGROUP} -s /bin/sh -D ${PHPUSER}
RUN sed -i "s/user = www-data/user = ${PHPUSER}/g" /usr/local/etc/php-fpm.d/www.conf
RUN sed -i "s/group = www-data/group = ${PHPGROUP}/g" /usr/local/etc/php-fpm.d/www.conf
RUN mkdir -p /var/www/html/public
RUN docker-php-ext-install pdo pdo_mysql
RUN apk add --no-cache $PHPIZE_DEPS \
    && pecl install xdebug \
    && docker-php-ext-enable xdebug
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]

And I can see that xDebug is running on port 9003 in my phpinfo() output.

How do I configure xDegug and PHPStorm now?

the_lar's avatar

After a lot of trial and error I have now solved this myself. Just writing here in the hope that this might help someone else, also for future reference for myself when I inevietably forget!

This works for a Dockerized Laravel app using PHP 8.1.8 with XDebug 3.1.5.

In my local project I have my xDebug options in php/php-dev.ini:

zend_extension=xdebug
xdebug.client_port = 9009
xdebug.idekey = PHPSTORM
xdebug.mode = develop,debug
xdebug.client_host = host.docker.internal

My docker-compose.yml files looks like this:

version: '3.8'
services:
  nginx:
    build:
      context: .
      dockerfile: nginx.dockerfile
    ports:
      - 80:80
    volumes:
      - ./src:/var/www/html
    depends_on:
      - mysql
      - php
  mysql:
    image: mariadb:10.5
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: laravel
      MYSQL_USER: laravel
      MYSQL_PASSWORD: secret
      MYSQL_ROOT_PASSWORD: secret
    volumes:
      - ./mysql:/var/lib/mysql
  php:
    build:
      context: .
      dockerfile: php.dockerfile
    volumes:
      - ./src:/var/www/html
      - ./php/php-dev.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
  composer:
    build:
      context: .
      dockerfile: composer.dockerfile
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html
  npm:
    image: node:current-alpine
    volumes:
      - ./src:/var/www/html
    entrypoint: ["npm"]
    working_dir: /var/www/html
  artisan:
    build:
      context: .
      dockerfile: php.dockerfile
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www.html
    depends_on:
      - mysql
    entrypoint: ["php", "/var/www/html/artisan"]

My php.dockerfile is this:

FROM php:8-fpm-alpine
ENV PHPGROUP=laravel
ENV PHPUSER=laravel
RUN adduser -g ${PHPGROUP} -s /bin/sh -D ${PHPUSER}
RUN sed -i "s/user = www-data/user = ${PHPUSER}/g" /usr/local/etc/php-fpm.d/www.conf
RUN sed -i "s/group = www-data/group = ${PHPGROUP}/g" /usr/local/etc/php-fpm.d/www.conf
RUN mkdir -p /var/www/html/public
RUN docker-php-ext-install pdo pdo_mysql
RUN apk add --no-cache $PHPIZE_DEPS \
    && pecl install xdebug \
    && docker-php-ext-enable xdebug
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
CMD ["php-fpm", "-y", "/usr/local/etc/php-fpm.conf", "-R"]

In PHPStorm my PHP - Debug settings are:

My PHP - Debug - DBGg Proxy:

Under PHP - Servers, there is a server with a name of _ this seems to have been created automatically! Not sure how that works but in there the settings are as follows:

Then under mappings I simply mapped the src folder in my local project to the /var/www/html folder on the container.

Sinnbeck's avatar

@the_lar very cool! Can I suggest putting it on some blog site? There is sadly a big chance that it will get buried here

the_lar's avatar

@Sinnbeck should do really, I don't have a blog though - do you know of any public space that I could use for this maybe?

Please or to participate in this conversation.