Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xdebug (inside Docker container) ignoring XDEBUG_CONFIG environment variable

I'm running a PHP application on Docker and I'd like to debug it using XDebug. In my docker-compose I added the following lines in the phpfpm part:

environment:
      XDEBUG_CONFIG: "remote_enable=1 remote_host=192.168.110.29 remote_port=9000 idekey=PHPSTORM remote_autostart=1"
      PHP_IDE_CONFIG: "serverName=reports.dev"

I configured PHPStorm in the right way, listening on port 9000 and ran the application.

The application works flawlessly but XDebug doesn't seem to be working.

If I move the lines of configuration inside the php.ini file the debugger works, except for the fact that Server Name is empty and I cannot debug (that's why I tried following the docker-compose configuration way).

If, inside the docker container, I run echo $XDEBUG_CONFIG the output is right, but XDebug seems not to read that Env variable.

like image 720
sangio90 Avatar asked Apr 10 '17 14:04

sangio90


People also ask

Do Docker containers inherit environment variables?

Using docker-compose , you can inherit env variables in docker-compose. yml and subsequently any Dockerfile(s) called by docker-compose to build images. This is useful when the Dockerfile RUN command should execute commands specific to the environment.

Can you use environment variables in Dockerfile?

Dockerfile provides a dedicated variable type ENV to create an environment variable. We can access ENV values during the build, as well as once the container runs.

How do I enable Xdebug log?

Enable Xdebug logging by adding the following line into php. ini: xdebug. remote_log=/log_path/xdebug.

How does Xdebug remote work?

When Xdebug is running, it will call back to your IDE (like PhpStorm or VS Code) from the server where it's running. Your IDE will sit and listen for that connection on a specific port (typically port 9000 or 9003).


2 Answers

I had the same problem. My image was based on nimmis/alpine-apache-php7/. I found that the image was using supervisor to start processes. supervisor has no knowledge of the Docker environment variables.

The convention to tell supervisor that a process needs to be run, is to create a run script at /etc/sv/{process}/run. A script like this was used to start Apache. I needed to change the script so that it would import Docker environment variables before starting Apache.

The docs for the base image explain the convention for importing Docker environment:

If you need environment variables from the docker command line (-e,--env=[]) add

source /etc/envvars

before you use them in the script file

So I created my own custom run script for Apache — I added source /etc/envvars just before execution of httpd.

I overwrote the original run script, by adding a simple COPY to my Dockerfile:

COPY apache-run.sh /etc/sv/apache2/run

This successfully ensured that my $XDEBUG_CONFIG was visible to httpd at the time it was launched. I was able to confirm that this affected my PHP configuration, by printing phpinfo(); in a webpage.

like image 104
Birchlabs Avatar answered Oct 15 '22 19:10

Birchlabs


Did you do a phpinfo();? That should tell you the settings used.

I had the same problem... took me forever to figure out. Are you using xdebug.remote_connect_back=1? If so remove that line.

My nginx proxy was forwarding an IP through (which was wrong) and so the ip set in XDEBUG_CONFIG was being ignored.

like image 28
that0n3guy Avatar answered Oct 15 '22 21:10

that0n3guy