docker nginx+php-fpm: Environment variable working on console but not in script

Viewed 2112

i'm using an ubuntu:18.04 container with nginx and php-fpm7.2. I set

clear_env=no

in /etc/php/7.2/fpm/pool.d/www.conf and

variables_order = "EGPCS"

in /etc/php/7.2/fpm/php.ini

I pass pass a variable $myVar from docker-compose. Running

echo $myVar

shows the correct value.

Running php -r "echo getenv('myVar');" shows me the correct value.

Placing a file index.php into /var/www/html/ (this is my www root) containing

<?php
   echo getenv('myVar');
?>

returns me an empty string.

Can someone help me?

Thank you, Martin

2 Answers

This is a PHP-FPM prank.

  • Setup the clear_env = no on /etc/php/7.2/fpm/pool.d/www.conf, so;

On Dockerfile start de php-fpm with init.d, not use service.
Ex:

CMD /etc/init.d/php7.2-fpm start && nginx -g 'daemon off;'

Check environment variables now

It's possible that the naming convention, more specifically, the separator is causing the issue. Try naming your env var MY_VAR instead of MyVar.

Related