Docker – Configure php-fpm to access environment variables in docker

dockerdocker-composeenvironment-variablesphp-fpm

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?

Best Answer

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 🤩

Related Topic