ConfigMaps as Files
Some apps do not read environment variables at all. They expect a configuration file, such as an nginx.conf or an app.yaml. A ConfigMap can deliver whole files too — and unlike env, those files can change while the pod runs.
The rule
Mount a ConfigMap and every key becomes a file.
Put a whole file's text under one key using a YAML block (|):
apiVersion: v1
kind: ConfigMap
metadata:
name: notes-web-files
namespace: notes
data:
app.yaml: |
theme: dark
max_note_length: 5000Then mount it as a volume:
spec:
containers:
- name: web
image: ghcr.io/example/notes-web:1.2
volumeMounts:
- name: config
mountPath: /etc/notes
volumes:
- name: config
configMap:
name: notes-web-filesInside the container, the file /etc/notes/app.yaml now exists with that text. Every key becomes one file named after the key.
A mounted directory replaces whatever the image had at that path. Mount into an empty or dedicated folder.
When the ConfigMap is edited, mounted files are refreshed automatically after a short delay (usually within a minute or two). Two exceptions:
- files mounted with
subPathare never refreshed; - the app itself must notice and reread the file — many only read config at start.
You can also build a ConfigMap straight from files on disk:
kubectl create configmap notes-web-files --from-file=app.yaml -n notesConfigMaps are meant to be small: the total is limited to 1 MiB.
The shortcut
Choose by how the app reads settings: individual variables → env; a whole file → a volume. Then ask "does it need live changes?" — only a mounted volume without subPath can give them.
How does it read?
notes-web loads
/etc/notes/app.yaml. That is a file, so use a volume.
The trap is mounting over a folder the app needs, such as /etc, which hides every other file the image put there.
Drills
Answer fast — trust the rule.
A ConfigMap with keys app.yaml and banner.txt is mounted at /etc/notes. What appears there?
The ConfigMap is edited. Which mounted file is never refreshed?
The image ships files in /etc/notes. You mount a ConfigMap at /etc/notes. What happens to the image's files?
An app only reads its config file at startup. The mounted file changes. What else is needed?
Remember
- Mount a ConfigMap and every key becomes a file.
- Variables → env; whole files or live changes → volume.
- Trap: mounts hide the image's folder, and
subPathmounts never refresh.
