This might be a dumb question, but since I can’t find a solution that works for me, here goes.
I have a docker image built from debian 11 with apache and php 8.1 installed.
I start the container with something like:
docker run -e MY_ENV_VAR=VALUE myphpimg bash
Once inside the container, I run "php -i" and can see all env variables, including "MY_ENV_VAR"
All good, right? Nope. If I try to acess the env variable inside a php script it simply does not work. I tried "getenv" as well as "$_ENV" and "$_SERVER".
Any ideas? Thanks.
2
Answers
Worked for me:
After days of messing around I got it to work but didn't fully understand it. My original image, built "from" debian 11 with apache and php 8.1 installed, had the following entrypoint:
I changed it to:
It worked.
I now believe that on start all docker environment variables are passed to apache, but if you restart apache for some reason you'll loose access to the variables. I Know that because after everything was working fine I restarted apache.
I ended up manually adding the env vars that I pass to docker to /etc/init.d/apache2:
Now everytime apache runs the env vars are passed on, even on restart. However, I must use "/etc/init.d/apache2 start" instead of "service apache2 start".
Bonus:
If you need to use $_ENV instead of getenv() edit "variables_order" in your php.ini
Common solution (php configuration):
Related configurations: How to get PHP to be able to read system environment variables