phplaraveldockerxdebugfpm

How to enable xdebug in php:7.4-fpm-alpine docker container?


My target is to use this git repo for Laravel with xdebug for php-fpm: https://github.com/aschmelyun/docker-compose-laravel

When using this repo i run:

  1. docker-compose up -d --build site
  2. docker-compose up

Here is the docker file from above repo:

FROM php:7.4-fpm-alpine

ADD ./php/www.conf /usr/local/etc/php-fpm.d/www.conf
RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel
RUN mkdir -p /var/www/html
RUN chown laravel:laravel /var/www/html
WORKDIR /var/www/html
RUN docker-php-ext-install pdo pdo_mysql

I have also added the port here (compose.dockerfile):

php:
    build:
      context: .
      dockerfile: php.dockerfile
    container_name: php
    volumes:
      - ./src:/var/www/html:delegated
    ports:
      - "9000:9000"
     # Added next line:
      - "9001:9001"                     
    networks:
      - laravel

I have tried add this to the end of php.dockerfile:

# Install essential build tools
RUN apk add --no-cache \
    git \
    yarn \
    autoconf \
    g++ \
    make \
    openssl-dev

# Install xdebug
RUN docker-php-source extract \
    && pecl install xdebug \
    && echo "xdebug.remote_enable=on\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_autostart=on\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_port=9001\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_handler=dbgp\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && echo "xdebug.remote_connect_back=1\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
    && docker-php-ext-enable xdebug \
    && docker-php-source delete \
    && rm -rf /tmp/*

This is the error I get when adding the above lines(It seems unrelated, but I guess it break some dependency):

mysql       | Version: '5.7.29'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
composer    | list [--xml] [--raw] [--format FORMAT] [--] [<namespace>]
composer    |
npm exited with code 1
composer exited with code

I have tried other things I found on google. However not been able to get it to work(since I really don´t understand what I do). I think the above feelt like it was the closed I got, but maybe I'm completly wrong.

I run it on Windows 10, any more information needed?


Solution

  • I found this instructions here on how to set it up. Add it to the end of the php.dockerfile:

    # Install base packages
    RUN apk update
    RUN apk upgrade
    
    # xdebug with VSCODE
    ENV XDEBUG_VERSION=2.9.2
    RUN apk --no-cache add --virtual .build-deps \
            g++ \
            autoconf \
            make && \
        pecl install xdebug-${XDEBUG_VERSION} && \
        docker-php-ext-enable xdebug && \
        apk del .build-deps && \
        rm -r /tmp/pear/* && \
        echo -e "xdebug.remote_enable=1\n\
            xdebug.remote_autostart=1\n\
            xdebug.remote_connect_back=0\n\
            xdebug.remote_port=9001\n\
            xdebug.idekey=\"VSCODE\"\n\
            xdebug.remote_log=/var/www/html/xdebug.log\n\
            xdebug.remote_host=host.docker.internal" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
    
    
    # Change TimeZone
    RUN apk add --update tzdata
    ENV TZ=Europe/Bucharest
    

    EDIT: You should also remove the xdebug port in docker-compose.yml (In case you added it)

    For **Visual Studio Code** Here is the kaunch.json I used:
    {
        // 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": 9001,
                "pathMappings": {
                    "/var/www/html/public": "${workspaceFolder}/src/public"
                },
            }
        ]
    }