skip to Main Content

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"

php -i return

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


  1. Chosen as BEST ANSWER

    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:

    ENTRYPOINT service apache2 restart && bash
    

    I changed it to:

    ENTRYPOINT /usr/sbin/apache2ctl -D BACKGROUND && bash
    

    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:

    edit /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


  2. Common solution (php configuration):

    clear_env = no
    

    Related configurations: How to get PHP to be able to read system environment variables

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search