Setup XDEBUG for PhpStorm and Docker-Apache2-PHP

https://gitlab.com/genadyb2020/docker

First, image with Apache2, git, php, composer
Dockerfile

FROM php:8.2-apache

# Enable Apache mod_rewrite
RUN a2enmod rewrite

# Install Git, unzip, and system dependencies
RUN apt-get update && apt-get install -y 
    git 
    unzip 
    zip 
    curl 
    && rm -rf /var/lib/apt/lists/*

# Install Composer
RUN curl -sS https://getcomposer.org/installer | php – –install-dir=/usr/local/bin –filename=composer

# Install xdebug (optional, if XDEBUG_VERSION is defined)
#ARG XDEBUG_VERSION=3.3.0
#RUN yes | pecl install xdebug-${XDEBUG_VERSION} 
#    && echo ”zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)” > /usr/local/etc/php/conf.d/xdebug.ini 
#    && echo ”xdebug.mode-debug” >> /usr/local/etc/php/conf.d/xdebug.ini

RUN pecl install xdebug && docker-php-ext-enable xdebug

# Set working directory
WORKDIR /var/www/html

Then let’s do some mounts, expose ports, settings:

services:
  web:
    build: .
    image: apache_php_xdebug
    ports:
      - ”80:80”    #this line maps your pc port to the container port
    volumes:
      - /d/Workspace/docker/mounts/etc/apache2/sites-enabled/:/etc/apache2/sites-enabled/
      - /d/Workspace/docker/mounts/usr/local/etc/php/conf.d/:/usr/local/etc/php/conf.d/
      - /d/Workspace/docker/mounts/logs/:/logs/
      - /d/Workspace/docker/mounts/tmp/:/tmp
      - /d/Workspace/gbond-blog/:/var/gbond-blog
    extra_hosts:
      - ”host.docker.internal:host-gateway”
    environment:
      XDEBUG_MODE: debug
      XDEBUG_CONFIG: client_host=host.docker.internal client_port=9003
      PHP_IDE_CONFIG: serverName=localhost

⚠️ Do not add mapping for xdebugger port (9003), it is treated as a listening port! ⚠️

ports:
     - ”9001:9001”  # Xdebug port for PHP 8+

⚠️ ⬆️ Obsoloete ⬆️⚠️