Search code examples
laraveldockerphpstormxdebug

How to properly config Docker to not require export PHP_IDE_CONFIG every time I debug


I'm trying to learn Laravel by putting my hands on a basic project from scratch. After some testing on other alternatives, I'm using this docker-nginx-php-laravel as a base platform.

It works like a charm on almost everything, but when I try to debug from a terminal I need to run

export PHP_IDE_CONFIG="serverName=localhost"

everytime, otherwise Xdebug/PhpStorm does not stop and throws this error:

"To fix it set server name by environment variable PHP_IDE_CONFIG and restart debug session."?

I don't mind doing it every time, but I'm certain that this is not the correct way to work, so I'd like to know what and where I need to configure on Docker so my terminal is identified by default.

I'm using Mac, and this is the Xdebug config I'm using:

[PHP]
zend_extension=xdebug

xdebug.mode = coverage,debug,develop
xdebug.client_port = 9003
xdebug.client_host = host.docker.internal
xdebug.idekey = PHPSTORM
#xdebug.show_error_trace = 1
xdebug.start_with_request = yes
xdebug.file_link_format = phpstorm://open?%f:%l
xdebug.discover_client_host= 1
xdebug.max_nesting_level = 10000
xdebug.log="storage/logs/xdebug.log"
xdebug.log_level = 0
xdebug.extended_info = On

According to Google I should modify bash config with

nano ~/.bash_profile

But I'm not clear on how to do that from Docker (or if that's even the best solution). The Dockerfile was left exactly as it was

Regarding the docker-compose, inspired by this post tried adding an "environment" line under "laravel" service, but it didn't work:

  environment:
    PHP_IDE_CONFIG: "serverName=localhost"

Any suggestions?


Solution

  • you should place the environment directive at the correct indentation level in your docker-compose.yml:

    services:
      laravel:
        image: your-image-name
        ports:
          - "80:80"
        environment:
          - PHP_IDE_CONFIG=serverName=localhost
        # ...
    

    or if you have a custom Dockerfile for your PHP service, you could also set like that:

    FROM php:7.4-fpm
    # ...
    
    ENV PHP_IDE_CONFIG "serverName=localhost"
    
    # ...
    

    If you continue to face issues, make sure you check the logs and verify that the variable is indeed being set within your Docker container:

    docker exec -it <your-container-name> bash
    echo $PHP_IDE_CONFIG