olle.holm
olle.holm

Reputation: 251

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?

Upvotes: 1

Views: 17045

Answers (3)

r1v3n
r1v3n

Reputation: 450

All of current stack solutions use pecl. If you don't want to do it, follow current answer.


You can also use docker-php-extension-installer to install anything you want without knowing it's version like that:

FROM php:7.4-apache

ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN chmod +x /usr/local/bin/install-php-extensions && \
    install-php-extensions gd xdebug

RUN apt-get update && apt-get install -y \
          libpng-dev \
          libonig-dev \
          libfreetype6-dev \
          libjpeg-dev \
          curl

RUN echo "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/xdebug.so\n\n[xdebug]\nxdebug.mode=debug\nxdebug.start_with_request=yes\nxdebug.client_host=\"host.docker.internal\"\nxdebug.client_port=9000\nxdebug.log=/tmp/xdebug.log\nxdebug.remote_enable=1\nxdebug.remote_autostart=1\n" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini

RUN docker-php-ext-configure gd --with-jpeg --with-freetype
RUN docker-php-ext-install pdo pdo_mysql mysqli gd mbstring
RUN echo "date.timezone = Europe/Moscow" >> /usr/local/etc/php/php.ini

That's all :_)

Upvotes: 0

Tania Petsouka
Tania Petsouka

Reputation: 1424

RUN apk add --no-cache $PHPIZE_DEPS \
    && pecl install xdebug-2.9.2 \
    && docker-php-ext-enable xdebug  \

Upvotes: 10

banankontakt
banankontakt

Reputation: 308

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"
            },
        }
    ]
}

Upvotes: 3

Related Questions