在Cloud Foundry容器中,.profile.d脚本中设置的导出的环境变量是否是持久的?

huangapple go评论66阅读模式
英文:

Are exported environment variables set in .profile.d scripts persistent in cloudfoundry container?

问题

我是在开发自定义构建包方面相当新手。我想设置一些容器级别的环境变量。我在/bin/supply目录下创建的脚本中添加了导出命令。

# 设置默认值
mkdir -p "${BUILD_PATH}/.profile.d/"
cat > "${BUILD_PATH}/.profile.d/defaults.sh" <<SH
export TEST_VARIABLE="hello"
SH

我了解到添加到.profile.d的脚本将在构建包启动期间被调用。我的容器按预期启动。我通过SSH连接到容器并验证了脚本位于/app/.profile.d/中。我还可以在应用程序启动日志中看到引用了该变量。但是当我尝试在容器内部echo该变量时,它为空。

我知道我可以使用cf set-env来设置这个变量,但我只是想知道在构建包中是否可能实现这一点。

英文:

I am quite new in developing a custom buildpack. I want to set some container level environment variables. I added the export commands in a created script under /bin/supply .

# setup defaults
mkdir -p &quot;${BUILD_PATH}/.profile.d/&quot;
cat &gt; &quot;${BUILD_PATH}/.profile.d/defaults.sh&quot; &lt;&lt;SH
export TEST_VARIABLE=&quot;hello&quot;
SH

I read that the scripts added to .profile.d will be sourced during buildpack launch. My container starts as expected. I ssh to the container and verified that the script was inside /app/.profile.d/ . I can also see in app start logs that the variable was referenced. But when I tried to echo the variable inside the container, it was empty.

I know that I can setup the variable using cf set-env but I just want to know if it is possible in buildpack.

答案1

得分: 1

以下是要翻译的内容:

"Believe it or not, things are working. Your buildpack is writing to the correct location, and the environment variable is being set for your application.

The issue is with cf ssh. When you cf ssh into an application it does not source the .profile.d/ scripts. Those are only sourced before the application starts.

The reason for this is that the system cannot know what is in those scripts, and it is possible that sourcing them when you cf ssh into the container could be harmful or cause side effects. Most of the time, that's not the case, and those scripts just set up the environment.

As such, if you want to enter the container and have the environment setup for you then you can run cf ssh &lt;app&gt; -t -c &quot;/tmp/lifecycle/launcher /home/vcap/app bash ‘’”. This will execute the launcher, which will source all those files.

Alternatively, you can cf ssh like normal and then specifically source the .profile.d scripts you require."

英文:

Believe it or not, things are working. Your buildpack is writing to the correct location, and the environment variable is being set for your application.

The issue is with cf ssh. When you cf ssh into an application it does not source the .profile.d/ scripts. Those are only sourced before the application starts.

The reason for this is that the system cannot know what is in those scripts, and it is possible that sourcing them when you cf ssh into the container could be harmful or cause side effects. Most of the time, that's not the case, and those scripts just set up the environment.

As such, if you want to enter the container and have the environment setup for you then you can run cf ssh &lt;app&gt; -t -c &quot;/tmp/lifecycle/launcher /home/vcap/app bash ‘’”. This will execute the launcher, which will source all those files.

Alternatively, you can cf ssh like normal and then specifically source the .profile.d scripts you require.

huangapple
  • 本文由 发表于 2023年6月2日 04:18:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76385436.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定