Using an environment variable in a file docker container

4/6/2020

I am setting an environment variable in a docker container whose value I receive at run time.

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

I want to use this value inside an XML file. 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>
-- The_Lost_Avatar
bash
docker
kubernetes
kubernetes-statefulset

2 Answers

4/7/2020

Neither Kubernetes nor Helm provide a way to template ConfigMaps or any other files. What you're probably looking for is init container, which starts before your container and which can execute any custom script you want. Then, you can modify the configuration file with your environment variable.

-- RafaƂ Leszko
Source: StackOverflow

4/7/2020

You could use envsubst and call it in your entrypoint.sh script. You will have to define your variable first and then substitute them with envsubst.

kminehart explaines very well how envsubst works in one of the github cases.

# 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. Here is a good article that shows how it works.

sed -i -g "s/[target_expression]/[replace_expression/g" <file> 
-- acid_fuji
Source: StackOverflow