Configure php-fpm to access environment variables in docker

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 🤩


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 -

  • This docker issue
  • This (more comprehensive) workaround