简体   繁体   English

在文件 docker 容器中使用环境变量

[英]Using an environment variable in a file docker container

I am setting an environment variable in a docker container whose value I receive at run time.我在 docker 容器中设置环境变量,我在运行时收到其值。

  env:
    - name: POD_INFO
      value: {{ Values.resolved_when_the_container_starts }}

I want to use this value inside an XML file.我想在 XML 文件中使用这个值。 Is there a way I can do this?有没有办法我可以做到这一点?

Something like就像是

<property>
   <name>pod.info</name>
   <value>this place should pick up the value from that environment variable</value>
</property>

Neither Kubernetes nor Helm provide a way to template ConfigMaps or any other files. Kubernetes 和 Helm 都没有提供模板化 ConfigMap 或任何其他文件的方法。 What you're probably looking for is init container , which starts before your container and which can execute any custom script you want.您可能正在寻找的是init container ,它在您的容器之前开始,并且可以执行您想要的任何自定义脚本。 Then, you can modify the configuration file with your environment variable.然后,您可以使用您的环境变量修改配置文件。

You could use envsubst and call it in your entrypoint.sh script.您可以使用 envsubst 并在entrypoint.sh脚本中调用它。 You will have to define your variable first and then substitute them with envsubst .您必须先定义变量,然后用envsubst替换它们。

kminehart explaines very well how envsubst works in one of the github cases . kminehart很好地解释了envsubst在 github 案例之一中的工作原理。

# mytemplate.tmpl  
apiVersion: extensions/v1beta1  
kind: Deployment  
# ...  
architecture: ${GOOS}  
GOOS=amd64 envsubst < mytemplate.tmpl > mydeployment.yaml  
# mydeployment.yaml  
apiVersion: extensions/v1beta1  
kind: Deployment  
# ...  
architecture: amd64  

Another way of env substition is sed .环境替换的另一种方式是sed Here is a good article that shows how it works. 是一篇很好的文章,展示了它是如何工作的。

sed -i -g "s/[target_expression]/[replace_expression/g" <file> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM