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

> kubectl create configmap --from-file builds a ConfigMap from files or a directory. Reference for --from-file, --from-literal, --from-env-file, and CI errors.

Source: https://latchkey.dev/learn/command-reference/kubectl-create-configmap-from-file  
Updated: 2026-06-30

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

| Flag | What 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 yaml | Render 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.

## Using this in CI

A runner has no kubeconfig, no cached context, and no interactive auth. Every kubectl invocation in CI needs the context supplied explicitly, and most confusing CI failures here are the command running against the wrong cluster or no cluster at all.

```Terminal
# never rely on the ambient context on a runner
kubectl --context "$KUBE_CONTEXT" -n "$NAMESPACE" get pods

# confirm what you are actually connected to before mutating anything
kubectl config current-context
kubectl cluster-info

# fail fast instead of hanging on an unreachable API server
kubectl --request-timeout=30s get nodes
```

> Always set `--request-timeout` in CI. Without it an unreachable API server hangs until the job times out, which turns a thirty-second failure into a twenty-minute one.

## FAQ

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

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.

### 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: ...

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
