php: Environment variables ignored by php-fpm
If you use the php-fpm variant, environment variables (e.g. for linked docker hosts) are not available in your $_ENV
var in your PHP scripts. This is a problem as usually docker containers are configured via environment variables (following the 12 factor principles). While this is rather a PHP problem, I still think that the official php-fpm image should provide a workaround for this, as otherwhise it’s pretty much useless.
For php-fpm all environment variables have to be listed in the php-fpm.conf
like:
[www]
env[MY_ENV_VAR_1] = 'value1'
env[MY_ENV_VAR_2] = 'value2'
So I suggest to create a wrapper script to first add all variables from env
to the configuration, before actually starting php.
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 16
- Comments: 34 (12 by maintainers)
Links to this issue
Commits related to this issue
- Tells FPM not to clear the environment in workers Allows php scripts executed with FPM to access env vars such as env vars from linked containers see #74 — committed to mathroc/php by mathroc 9 years ago
- Added a simpler way to get env vars in php-fpm Answer was discussed here https://github.com/docker-library/php/issues/74 not and expert though — committed to select/documentation by deleted user 6 years ago
- Add xdebug As a necessary step, enable all environment variables to be seen in PHP. See https://github.com/docker-library/php/issues/74 — committed to wmilewski/docker-php by wmilewski 6 years ago
- Added a simpler way to get env vars in php-fpm Answer was discussed here https://github.com/docker-library/php/issues/74 not and expert though — committed to nextcloud/documentation by deleted user 6 years ago
- Fix environment variable ignored by php-fpm (reference : https://github.com/docker-library/php/issues/74) — committed to misterabdul/alpine-supervisor by misterabdul 3 years ago
If you’re going to re-expose the whole environment,
clear_env = no
would be simpler.👍 for
clear_env = Yes
by defaultoops that was a mistake 😕 I meant 👍 for
clear_env = no
by default… as for the reason, it’s because I need to add it manually in each of my php-fpm containers, I guess a lot of people have to do the same or worst are manually adding each need env var in php-fpm conf as you did at first. it’s not that easy to find information aboutclear_env
when you search for “php-fpm env var” on googleHere’s a workaround that does the job for me. It would be great if we could include that in the php-fpm image:
run-php-fpm:
@deiucanta
In the case you were starting
php-fpm
not in a container but withsystemd
you need to said it where to get environment variables. For example set in/lib/systemd/system/php7.2-fpm.service
:ENV[*]
from fpm config can be reached as$_SERVER[*]
in PHP.This is the same default that is in debian when installing the
php5-fpm
package, as well as the default in php (php.net). There is no corresponding setting for non-fpm php.That said, since php is the only process in the container, I think it would be sane to change from upstream’s default and add
clear_env = no
, since then php will have access to things like linked sql address and port variables.This is strange - I’ve uncommented
clear_env = no
but I can’t access ENV vars inside PHP.For those who still don’t have access to
$_ENV
, thisphp.ini
setting is (now) important (in e.g.php:8.0-fpm-alpine
).from
php.ini-production
:From http://php.net/manual/en/install.fpm.configuration.php:
You’d put it in your
php-fpm.conf
or a file included from it.FWIW, I don’t think the
php:fpm
image should be changed to act as you’ve requested, but I do think the environment variable issue should be documented.For those of us on CentOS 7 who can’t use
clear_env = no
(php-fpm is just a tiny bit too old), mikehaertl’s comment regarding the workaround withsed
is a lifesaver.clear_env = no
worksHi!
just googled this issue and it looks like a good place for my 5 cents.it’s not according this concrete docker image, but a caution to a docker image writers:
many docker images are built so that all passwords are configured during the first
run
. This means you specify passwords that are available as environment variables in a running container during all docker container life cycle. For containers like the ones withPHP
or any other server-side scripting language inside which have access to environment variables this looks like a security issue: anyone who can eval some code on the server may read environment variables (if allowed, actually. But tuning this off may be forgotten, or be changed in a running container without recalling the side effects or by another person who tries to solve some other issue).So personally I decided that it should be always separated. I build my own containers with all sensitive data being provided during the build step. Then I run it without providing any passwords. Yes, not very share-friendly and not that much convenient, but it works.
Please let me know if I’m missing something, or if you feel like I’m doing this completely wrong. Thanks!