
If you are not familiar with the oc command, refer to OpenShift - Getting Started with the oc command.
There are different ways to configure a container with environment variables.
- In a deployment YAML file (this article)
- Using Config Map
- Using Secrets
The oc set env command can be used to create environment variables for a deployment.
~]$ oc set env deployment my-deployment my-environment-variable="Hello World"
deployment.apps/my-deployment updated
The oc set env command simply updates the deployments template YAML file to have the environment variable, so you could just as well use the oc edit command to update a deployments template YAML file to have the environment variable.
spec:
containers:
- name: my-app
env:
- name: my-environment-variable
value: "Hello World"
A new pod should immediately be created after the oc edit or oc set env commands have been issued, which can be seen with the AGE of the oc get pods command.
~]# oc get pods
NAME READY STATUS RESTARTS AGE
my-pod-276pc 1/1 Running 0 22s
And the oc describe pod command can be used to see that the pod is now has the environment variable.
~]$ oc describe pod my-pod-276pc
Containers:
my-container:
Environment:
my-environment-variable: "Hello World"
The oc set env command with the --list option can be used the list the environment variables associate with the deployment.
~]$ oc set env deployment my-deployment --list
my-environment-variable="Hello World"
And the pod.
~]$ oc set env pod my-pod-276pc --list
my-environment-variable="Hello World"
And the replica set.
~]$ oc set env replicaset my-deployment-5755cd7b85 --list
my-environment-variable="Hello World"
Also, the oc exec command and the env or printenv command can be used to see if the "my_secret" variable contains a value of "Hello" in the container.
~]$ oc exec/my-pod-276pc -- env
my-environment-variable="Hello World"
Did you find this article helpful?
If so, consider buying me a coffee over at