9

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?

2 Answers 2

11

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 🤩

2
  • Wtf this actually worked, what the fuck php-fpm
    – Shardj
    Mar 31, 2020 at 15:26
  • It's working, dude. Why is it possible? Any references?
    – nmfzone
    Sep 13, 2021 at 7:03
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 -

6
  • 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. Nov 5, 2016 at 16:45
  • 1
    Setting clear_env=no before starting php-fpm didn't work for me. The env var's still weren't available.
    – Aidan Ewen
    Nov 7, 2016 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 7, 2016 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 Jul 11, 2017 at 22:55
  • 1
    I can confirm that clear_env = no absolutely isn't working for me on ubuntu
    – Shardj
    Mar 31, 2020 at 15:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .