Environment variable in PHP-docker container
Asked Answered
H

3

12

I want to show an env var in my docker container. The PHP script looks like this:

<html>
 <head>
  <title>Show Use of environment variables</title>
 </head>
 <body>
  <?php
  print "env is: ".$_ENV["USER"]."\n";
  ?>
 </body>
</html>

I use OpenShift to start the container. The PHP - container shows:

env is: 

Now I change the dc config of my container:

oc env dc/envar USER=Pieter
deploymentconfig "envar" updated

When I access the container. The env var of USER is Pieter

docker exec -it 44a0f446ae36 bash
bash-4.2$ echo $USER
Pieter

But my script remains showing: "env is:" It does not fill in the variable.

Heiney answered 18/2, 2016 at 10:48 Comment(2)
What does the log shows?Depot
did anyone find a solution?Hypermeter
N
13

Change

print "env is: ".$_ENV["USER"]."\n";

to

print "env is: ".getenv("USER")."\n";

.

test.php

<html>
 <head>
  <title>Show Use of environment variables</title>
 </head>
 <body>
  <?php
  print "env via \$_ENV is: ".$_ENV["USER"]."\n";
  print "env via getenv is: ".getenv("USER")."\n";
  ?>
 </body>
</html>
# export USER=Sascha
# echo $USER
Sascha

php test.php

    <html>
     <head>
      <title>Show Use of environment variables</title>
     </head>
     <body>
      PHP Notice:  Array to string conversion in /test.php on line 7
    PHP Notice:  Undefined index: USER in /test.php on line 7
    env via $_ENV is: 
    env via getenv is: Sascha
     </body>
    </html>
Needlecraft answered 19/2, 2016 at 16:40 Comment(1)
Worked for me. That was the answer I was looking for!Plaintiff
H
1

I did the following to solve this issue:

  1. Use this command that writes the environment variables to a file called .env in your project folder.

    CMD ["env >> /path/to/project/.env"]
    
  2. Install dotenv package that loads the env variables from a file to $_ENV

    composer require vlucas/phpdotenv
    
  3. Load the package and use it as

    require 'vendor/autoload.php'
    $dotenv = new Dotenv\Dotenv(__DIR__);
    $dotenv->load();
    getenv('VAR_NAME');
    

Hope it helps.

Hypermeter answered 20/6, 2018 at 8:25 Comment(2)
Use $_SERVER['VAR_NAME'] instead of getenv() or $_ENV when accessing environmental variables, to be thread-safe and follow good practice.Onym
The comment from @Onym should be the right answer, depending on how those variables are set can appear either on getenv or $_ENV and you can get ending will NULL or FALSE since the variable may not be set. But using $_SERVER will work alwaysSubmediant
A
-2

You just need to use different quotes, it is working to me with this

<html>
 <head>
  <title>Show Use of environment variables</title>
 </head>
 <body>
  <?php
  print "env is: ".$_ENV['USER'];
  ?>
 </body>
</html>
Augustin answered 17/10, 2020 at 9:26 Comment(3)
Please explain that further - which "different quotes" do you mean? $_ENV["USER"] and $_ENV['USER'] should perform the exact same result. If not, please open a bug reportBrendin
He used print "env is: ".$_ENV["USER"]."\n". Index should be in single quotes not in double quotes. I tested it on my computer and it works.Augustin
What makes you think that there is any difference between using single or double quotes? Are you sure that your code does not work when using double quotes? Then please file a bug reportBrendin

© 2022 - 2024 — McMap. All rights reserved.