Configure php-fpm to access environment variables in docker
Solution 1
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 🤩
Solution 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 -
Related videos on Youtube
Comments
-
Aidan Ewen over 1 year
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')
returnsFALSE
.I've changed
/etc/php/7.0/fpm/pool.d/www.conf
to includeclear_env = no
and restarted withservice 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?
-
Michael Hampton over 7 yearsOr 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. -
Aidan Ewen over 7 yearsSetting
clear_env=no
before starting php-fpm didn't work for me. The env var's still weren't available. -
Aidan Ewen over 7 years@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. -
David Duncan almost 7 yearsThis 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 almost 7 years@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!
-
Shardj about 4 yearsI can confirm that clear_env = no absolutely isn't working for me on ubuntu
-
Shardj about 4 yearsWtf this actually worked, what the fuck php-fpm
-
nmfzone over 2 yearsIt's working, dude. Why is it possible? Any references?