PHP-docker容器中的环境变量 [英] Environment variable in PHP-docker container

查看:208
本文介绍了PHP-docker容器中的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的docker容器中显示一个env var. PHP脚本如下所示:

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>

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

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

当我访问容器时. USER的环境变量是Pieter

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

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

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

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

推荐答案

更改

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

这篇关于PHP-docker容器中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆