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
- Using Config Map (this article)
- Using Secrets
Config Maps are used to:
- mount configuration files in a container
- create environment variables in a container
- create command line option arguments in a container
Secrets are similar, used to create variables that contain encoded data (e.g. passwords). In this way, if a change is needed to a configuration file, variable, or command line option argument, you just need to update the config map or secret as opposed to having to make the change to your applications or deployments.
flowchart TB
subgraph OpenShift["OpenShift Cluster"]
CM[ConfigMap
Configuration Data]
SEC[Secret
Sensitive Data]
subgraph Pod["Pod"]
subgraph Container["Container"]
APP[Application]
VOL1[/Volume Mount
Config Files/]
VOL2[/Volume Mount
Secret Files/]
ENV1[Environment Variables
from ConfigMap]
ENV2[Environment Variables
from Secret]
end
end
end
CM -->|Mount as Volume| VOL1
SEC -->|Mount as Volume| VOL2
CM -->|Inject as EnvVar| ENV1
SEC -->|Inject as EnvVar| ENV2
VOL1 -.->|Read Config| APP
VOL2 -.->|Read Secrets| APP
ENV1 -.->|Use Config| APP
ENV2 -.->|Use Secrets| APP
style CM fill:#90CAF9
style SEC fill:#FFAB91
style APP fill:#A5D6A7
style Pod fill:#E1F5FE
style Container fill:#F1F8E9
With the oc create configmaps command, there are two different command line options that can be used.
- --from-literal
- --from-file
Here is an example of how to create a config map using the --from-literal option. This is typically used to create a config map that will contain variables.
~]$ oc create configmap my-variables --from-literal foo="Hello" --from-literal bar="World"
configmap/my-variables created
Here is how you would create a config map using the --from-file option. This is typically used to create a config map that contains the contents of a configuration file. It is important to recongize that the name of the file will be used as the "key" in the config map. In this example, the key in the config map will be "server.xml" and the value will be the content of the server.xml file.
~]# oc create configmap server-xml-file --from-file server.xml
configmap/server-xml-file created
Or a YAML file that contains key value pairs can be used to create a Config Map. These files are known as templates. For example, let's say you have a YAML file named configmap.yml that contains the following markup.
apiVersion: v1
kind: ConfigMap
metadata:
name: my-variables
namespace: my-project
data:
foo: hello
bar: world
The oc apply or oc create command with the -f or --filename option can be used to create the config map using the template JSON or YAML file.
The oc replace command can be used to replace a config map using a new or updated template JSON or YAML file.
The oc edit command can be used to update an config map template YAML file.
~]$ oc apply --filename configmap.yml
configmap/my-variables created
Or in the OpenShift console, at Workloads > ConfigMaps > Create ConfigMap.

The oc get configmaps command can be used to list the config maps that have been created.
~]$ oc get configmaps
NAME DATA AGE
my-variables 1 44d
server-xml-file 1 44d
The --output yaml or --output json options can be used to display the YAML or JSON details of the config map. Notice that the "foo" key contains "hello" and "bar" contains "world".
~]$ oc get configmap my-variables --output yaml
apiVersion: v1
data:
foo: hello
bar: world
kind: ConfigMap
And perhaps server-xml-file contains something like this.
~]$ oc get configmap server-xml-file --output yaml
apiVersion: v1
data:
server.xml: |
<?xml version='1.0' encoding='utf-8'?>
<Server port="8080" />
kind: ConfigMap
Let's say you have a deployment named "my-deployment".
~]# oc get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
my-deployment 1/1 1 1 8d
The oc edit deployment command can then be used to update the deployment YAML to use the config maps.
AVOID TROUBLE
The name of the config map, which is server-xml-file in this example, must be an exact match of the name of the config map returned by the oc get configmaps command.
If subPath is not used, example.txt will be mounted as a directory. subPath must be used to mount example.txt as a file.
apiVersion: v1
kind: Deployment
spec:
replicas: 1
template:
spec:
containers:
- envFrom:
- configMapRef:
name: my-variables
name: my-container
volumeMounts:
- name: server-xml-file
mountPath: /data/conf/server.xml
subPath: server.xml
volumes:
- name: server-xml-file
configMap:
name: server-xml-file
Did you find this article helpful?
If so, consider buying me a coffee over at 