PHP-docker容器中的环境变量

我想在我的docker容器中显示一个env var.

PHP脚本如下所示:

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

我使用OpenShift启动容器. PHP – 容器显示:

env is: 

现在我更改了容器的dc配置:

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

当我访问容器时. USER的env var是Pieter

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

但我的脚本仍然显示:“env是:”它没有填写变量.

最佳答案 更改

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

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

.

/# cat 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>
/ # 
点赞