Configuring Xdebug3 for a Laravel Application in Docker

Let's start with the structure in which everything will be:





docker/
├── docker-compose.yml
├── .env
├── .env.example
├── .gitignore
└── services
    ├── database
    │   ├── dump
    │   └── .gitignore
    ├── nginx
    │   └── site.conf
    └── php
        ├── Dockerfile
        └── php.ini
      
      







And now about everything in order:





The file .gitingore



contains only one line/.env







The file .env.example



at the beginning of the project is the same as.env







The file docker-compose.yml



contains information about all our services:





version: "3.7"
services:
  php:
    build:
      args:
        uname: ${PHP_UNAME}
        uid: ${PHP_UID}
        gid: ${PHP_GID}
      context: ./services/php
    container_name: ${PROJECT_NAME}_php
    image: ${PROJECT_NAME}_php
    restart: unless-stopped
    working_dir: /var/www/
    volumes:
      - ./services/php/php.ini:/usr/local/etc/php/php.ini
      - ../:/var/www
    environment:
      COMPOSER_MEMORY_LIMIT: 2G
      XDEBUG_CONFIG: client_host=${XDEBUG_REMOTE_HOST} client_port=${XDEBUG_STORM_PORT} remote_enable=1
      PHP_IDE_CONFIG: serverName=${XDEBUG_STORM_SERVER_NAME}
    networks:
      - main_network
    depends_on:
      - db
  db:
    image: mysql:5.6
    restart: unless-stopped
    container_name: ${PROJECT_NAME}_db
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_USER: ${DB_USERNAME}
    ports:
      - ${DB_LOCAL_PORT}:3306
    volumes:
      - ./services/database/dump:/var/lib/mysql
    networks:
      - main_network

  nginx:
    image: nginx:1.17-alpine
    restart: unless-stopped
    container_name: ${PROJECT_NAME}_nginx
    ports:
      - ${NGINX_LOCAL_PORT}:80
    volumes:
      - ../:/var/www
      - ./services/nginx:/etc/nginx/conf.d
    networks:
      - main_network
    depends_on:
      - php
networks:
  main_network:
    driver: bridge
    name: ${PROJECT_NAME}_main_network
    ipam:
      driver: default
      config:
        - subnet: ${SUBNET_IP}/${SUBNET_MASK}

      
      



./services/php/php.ini:/usr/local/etc/php/php.ini



- php.ini



( ) . XDEBUG_CONFIG



- php



, xdebug -. client_host



- , xdebug . , PhpStorm . , , . , , XDEBUG_REMOTE_HOST



. CLIENT_PORT



, IDE (9003). XDEBUG_STORM_SERVER_NAME



- , IDE . , PhpStorm, ( , ; volumes ).









, .env



:





PROJECT_NAME=my_project

DB_DATABASE=my_project_db
DB_USERNAME=my_project
DB_PASSWORD=p@$$w0rd
DB_ROOT_PASSWORD=toor

PHP_UNAME=dev
PHP_UID=1000
PHP_GID=1000

DB_LOCAL_PORT=3377
NGINX_LOCAL_PORT=8077

XDEBUG_STORM_SERVER_NAME=Docker
XDEBUG_REMOTE_HOST=192.168.227.1
XDEBUG_STORM_PORT=9003

SUBNET_IP=192.168.227.0
SUBNET_MASK=28

      
      







, 192.168.227.0



28, - 32 - 28 = 4



, 2 ** 4 - 1 = 15



. 16 , , , 192.168.227.1



. XDEBUG_REMOTE_HOST



.









- site.conf



:





server {
    listen 80;
    server_name 127.0.0.1 localhost;
    client_max_body_size 5m;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    root /var/www/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}


      
      







Dockerfile ( php) xdebug. 2 :





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



, (- 3) ( 2).





Dockerfile



:





FROM php:7.4-fpm

# Arguments defined in docker-compose.yml
ARG uname
ARG gid
ARG uid

# Install system dependencies
RUN apt-get update \
    && apt-get install -y \
        git \
        curl \
        dpkg-dev \
        libpng-dev \
        libjpeg-dev \
        libonig-dev \
        libxml2-dev \
        libpq-dev \
        libzip-dev \
        zip \
        unzip \
        cron

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

RUN docker-php-ext-configure gd \
  --enable-gd \
  --with-jpeg

ADD ./php.ini /usr/local/etc/php/php.ini

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Install PHP extensions
RUN docker-php-ext-install pdo pdo_mysql pdo_pgsql pgsql mbstring exif pcntl bcmath gd sockets zip

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands
RUN groupadd --gid $gid $uname
RUN useradd -G www-data,root -s /bin/bash --uid $uid --gid $gid $uname

RUN mkdir -p /home/$uname/.composer && \
    chown -R $uname:$uname /home/$uname

# Set working directory
WORKDIR /var/www

USER $uname

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

      
      







, php.ini



:





max_execution_time=1000
max_input_time=1000
xdebug.mode=debug
xdebug.log="/var/www/xdebug.log"
xdebug.remote_enable=1
      
      







:





docker-compose up -d
      
      







PhpStorm :









Docker c (/var/www/quizzy.loc



) , (/var/www



):





, php :





Configuring php interpreter
php interpreter
Select the service where php is located and specify the path to the interpreter
, php
Setting launch parameters (via exec)
( exec)

Now we can go to the "Debug" sub-item and configure the port on which to run:





Setting up a debugger
Setting up a debugger

Let's check if Xdebug works:





Now we can put a breakpoint in our code and start listening for incoming connections:









Go to our favorite Firefox browser, install and enable the plugin on the desired page:





Reload the page, and PhpStrom should catch the connection:












All Articles