Skip to content
Latchkey

kubectl create configmap --from-file: Config from Files

kubectl create configmap --from-file loads file contents into a ConfigMap, keyed by filename or by a key you choose.

When config lives in files (nginx.conf, app.yaml), this packs them into a ConfigMap without copying contents into a manifest by hand.

What it does

kubectl create configmap with --from-file reads each file and stores its contents under a key (the basename by default, or key=path to rename). Pointing --from-file at a directory adds every file in it. --from-env-file parses KEY=value lines into individual entries.

Common usage

Terminal
kubectl create configmap app-config --from-file=./config/
kubectl create configmap nginx --from-file=nginx.conf=./conf/nginx.conf
kubectl create configmap flags --from-literal=LOG_LEVEL=debug
kubectl create configmap env --from-env-file=app.env
# idempotent YAML for apply
kubectl create configmap app-config --from-file=./config/ \
  --dry-run=client -o yaml | kubectl apply -f -

Options

FlagWhat it does
--from-file=<path>File (key=basename) or directory of files
--from-file=<key>=<path>Store a file under a custom key
--from-literal=<k>=<v>Add a single inline key/value
--from-env-file=<file>Parse KEY=value lines into entries
--dry-run=client -o yamlRender YAML without applying

In CI

Pipe --dry-run=client -o yaml into kubectl apply -f - to make the ConfigMap idempotent across reruns. Remember a ConfigMap is not encrypted and has a 1 MiB size limit; large binaries or secrets do not belong here.

Common errors in CI

"error: configmaps \"app-config\" already exists" on a second run means plain create is not idempotent; use the dry-run plus apply pattern. "error: <file> is not a valid key name for a ConfigMap" means the filename has characters outside [-._a-zA-Z0-9]; rename it or set an explicit key. "The ConfigMap is invalid: ... must have at most 1048576 bytes" means the files exceed the 1 MiB limit.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →