8

I'm running php7-fpm in a docker container. However my php scripts aren't able to access environments variables set in my docker-compose file. getenv('MY_ENV_VAR') returns FALSE.

I've changed /etc/php/7.0/fpm/pool.d/www.conf to include clear_env = no and restarted with service php7.0-fpm restart but my environment variables start aren't there.

I've also tried editing /etc/php/7.0/fpm/php.ini includes the line variables_order = "EGPCS".

When I exec into my container on a bash shell I can see that my variables exist. It's just that they're not accessible in my php scripts.

What am I missing?

Aidan Ewen
  • 271
  • 1
  • 4
  • 11

2 Answers2

10

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

Thiago Pereira
  • 201
  • 2
  • 4
2

Well this seems all wrong but I've got it working by adding the environment variables in a bash script -

#!/bin/bash    
echo "" >> /etc/php/7.0/fpm/pool.d/www.conf # new line.
if ! [ -z "$MY_ENV_VAR" ]
then
    echo "env[MY_ENV_VAR] = $MY_ENV_VAR;" >>  /etc/php/7.0/fpm/pool.d/www.conf
fi

Then in my Dockerfile -

COPY add_env_vars.sh /add_env_vars.sh
CMD source /add_env_vars.sh && service php7.0-fpm start

It looks like php-fpm just doesn't play well with system environment variables.

For more info see -

Aidan Ewen
  • 271
  • 1
  • 4
  • 11
  • Or you could have just used the simple `clear_env = no` which ought to be set in the container image already, but isn't because that guy doesn't understand it. – Michael Hampton Nov 05 '16 at 16:45
  • Setting `clear_env=no` before starting php-fpm didn't work for me. The env var's still weren't available. – Aidan Ewen Nov 07 '16 at 11:15
  • @MichaelHampton - thanks for your input. Am I missing something? The first thing I did was set `clear_env=no` in my container image. I didn't include that information in order to keep my question concise. (I stated that doing the same thing more directly didn't work - i.e. I exec'd into the container and ran the commands in a bash shell). Apologies if that wasn't clear - please do let me know if I'm still missing something. – Aidan Ewen Nov 07 '16 at 18:08
  • This is pretty old but clear_env does not work on RHEL based distros last I heard - That would explain the confusion here @MichaelHampton – David Duncan Jul 11 '17 at 22:55
  • @MichaelHampton My apologies... pretty rude though for a mod though don't you think? I guess I can explain why I left a comment: 1) In my cursory glance I didn't notice anything directly attributing this to Debian or RHEL 2) It's a pretty commonly known issue that clear_env really only works on debian based systems - This guy claimed that it didn't work and I was leaving a comment for future visitors in case they found this thread with the same issue (the same way I did) Have a great day and I apologize for tagging you with information! – David Duncan Jul 14 '17 at 19:39
  • I can confirm that clear_env = no absolutely isn't working for me on ubuntu – Shardj Mar 31 '20 at 15:24