3

I have started to play with my first Laravel project on a MacOS. I am using Laravel Sail for running the project inside a container and everything seems to work except the debugging part.

Versions used:

  • Laravel 8.66,
  • PHP 8.0.12,
  • Xdebug 3.1.1

According to Laravel docs we can set SAIL_XDEBUG_MODE to enable the debug mode. However, the debugging is not working in PhpStorm even if it is set to listen for incoming connections. I have seen that running php -i in container returns different configs for Xdebug comparing to what we see in phpinfo() from browser even if the Configuration file is the same one.

Here are some screenshots from browser:

WEB - PHP configuration files WEB - PHP Xdebug configs

Here are screenshots from what I see in the command line:

CLI - PHP configuration files CLI - PHP Xdebug configs

As it can be seen in the attached screenshots, in console is used the XDEBUG_MODE env var, while in browser is used xdebug.mode. Is is possible to have the same configs in console and browser via SAIL_XDEBUG_MODE ?

This is the Dockerfile that is used:

FROM ubuntu:21.04

LABEL maintainer="Taylor Otwell"

ARG WWWGROUP

WORKDIR /var/www/html

ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC

RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN apt-get update \
    && apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2 \
    && mkdir -p ~/.gnupg \
    && chmod 600 ~/.gnupg \
    && echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf \
    && apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E5267A6C \
    && apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C300EE8C \
    && echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu hirsute main" > /etc/apt/sources.list.d/ppa_ondrej_php.list \
    && apt-get update \
    && apt-get install -y php8.0-cli php8.0-dev \
       php8.0-pgsql php8.0-sqlite3 php8.0-gd \
       php8.0-curl php8.0-memcached \
       php8.0-imap php8.0-mysql php8.0-mbstring \
       php8.0-xml php8.0-zip php8.0-bcmath php8.0-soap \
       php8.0-intl php8.0-readline php8.0-pcov \
       php8.0-msgpack php8.0-igbinary php8.0-ldap \
       php8.0-redis php8.0-swoole php8.0-xdebug \
    && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && curl -sL https://deb.nodesource.com/setup_16.x | bash - \
    && apt-get install -y nodejs \
    && curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
    && echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
    && apt-get update \
    && apt-get install -y yarn \
    && apt-get install -y mysql-client \
    && apt-get install -y postgresql-client \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.0

RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail

COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/8.0/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container

EXPOSE 8000

ENTRYPOINT ["start-container"]

And this is my docker-compose.yml:

# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
            PHP_IDE_CONFIG: "serverName=my.local"
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
    mysql:
        image: 'mysql:8.0'
        ports:
            - '${FORWARD_DB_PORT:-3306}:3306'
        environment:
            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
            MYSQL_DATABASE: '${DB_DATABASE}'
            MYSQL_USER: '${DB_USERNAME}'
            MYSQL_PASSWORD: '${DB_PASSWORD}'
            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
        volumes:
            - 'sailmysql:/var/lib/mysql'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
            retries: 3
            timeout: 5s
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local

4 Answers 4

4

As it can be seen in the attached screenshots, in console is used the XDEBUG_MODE env var, while in browser is used xdebug.mode.

Xdebug uses XDEBUG_MODE if it is set, otherwise it falls back to the value of the xdebug.mode setting.

I don't know which web server Sail uses, but some will strip out the environment variables — including XDEBUG_MODE.

So you can either fix that, or update the Dockerfile to add a line below:

COPY php.ini /etc/php/8.0/cli/conf.d/99-sail.ini

Which says:

COPY xdebug.ini /etc/php/8.0/cli/conf.d/999-xdebug.ini

And add to the contents of xdebug.ini (in the same directory as php.ini):

xdebug.mode=develop,debug
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Derick. This is the second solution that I have tried, The debugger works as expected if configs are added in xdebug.ini, but I am still curios why the approach from Laravel docs with SAIL_XDEBUG_MODE is not working for debugging web calls as I am using only tools provided directly by Laravel.
I can't help much there, as Laravel Sail is not something I've any influence over. You could perhaps send them a bug report, as I think you're not the only one having this issue with them.
1

I had the same problem and I'm also on macOS.

You must use the export command, like this:

export SAIL_XDEBUG_MODE=develop,debug

3 Comments

Can this be added in the docker-compose.yml or it needs to be added in Dockerfile or .ini files ?
@AlexDomsa I just execute this command in Zsh.
thank you. I know that we can run commands in terminal, but I was wondering why those are not working from docker files.
0

in ~/.env, add:

SAIL_XDEBUG_MODE=debug

You can see in the docker-compose file it uses the value of this variable, or off by default.

XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'

You'll probably have to restart your docker containers for it to take affect.

2 Comments

I remember that I have tried this at the time this question was posted, but it was not working as expected. Will need to retry at some pointt
FYI this does not work. The SAIL_XDEBUG_MODE envvar currently does nothing. There was a PR with a proposed fix at github.com/laravel/sail/pull/358 but Taylor refused to merge it in for some reason.
0

I'm using Visual Studio Code as my editor and debugger. and other than changing the the value of SAIL_XDEBUG_MODE to develop,debug in my .env file the only other change I had to make to get this working was to update the launch.json file to add a path mapping from the path where the code is on the container to where it is on my local hdd.

The config for "Listen for Xdebug" looks like this:

   {
      "name": "Listen for Xdebug",
      "type": "php",
      "request": "launch",
      "port": 9003,
      "pathMappings": {
        "/var/www/html": "${workspaceFolder}"
      }
    },

I didn't have to make any changes to the docker container config or the Dockerfile itself, I havn't even exported it.

I'm guessing from the tag on the question that OP is using PHPStorm, and they have instructions on their site for setting the path mapping too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.