我想在我的 docker 容器中显示一个环境变量。
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 的环境变量是 Pieter
docker exec -it 44a0f446ae36 bash
bash-4.2$ echo $USER
Pieter
但我的脚本仍然显示:“env is:
" 它不填充变量。
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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)