How to convert a .env file to a ConfigMap or Secret
- Paste your
.envfile, or pick an example. Choose ConfigMap or Secret, a name, and optionally a namespace and labels. - If a line has a key and no
=, kubectl takes its value from your shell. Type it under Values from your shell, paste the output ofenv, or mark the key unset. These values stay in the tab's memory: never in the URL, browser storage or analytics, and Reset clears them. - Read the manifest next to the file: it is the YAML that
kubectl createwith--from-env-fileand--dry-run=client -o yamlprints, with the same key order and quoting. If kubectl would refuse the file, you see its exact error and the line. - Check What kubectl stores and the Docker Compose / dotenv differences, then copy or download the manifest — or copy the command and run it yourself.
- To go the other way, switch to manifest → .env and paste a ConfigMap or Secret.
Parsing rules: kubectl vs Docker Compose vs dotenv
The same file reads three different ways. Each cell below is computed by this page's engines: kubectl's env-file reader, a port of Docker Compose's parser (compose-go) and the npm dotenv parser.
| Line | kubectl stores | Docker Compose | dotenv |
|---|---|---|---|
Quotes around a valueGREETING="hello world" | "hello world" | hello world | hello world |
Single quotesTOKEN='abc 123' | 'abc 123' | abc 123 | abc 123 |
Escape sequencesMESSAGE="line 1\nline 2" | "line 1\nline 2" | line 1↵line 2 | line 1↵line 2 |
Inline comment after a spaceTIMEOUT=30 # seconds | 30 # seconds | 30 | 30 |
# inside a valueURL=https://example.test/#top | https://example.test/#top | https://example.test/#top | https://example.test/ |
export prefixexport REGION=eu-west-1 | error: not a valid key name | eu-west-1 | eu-west-1 |
Spaces around =NAME = web | error: not a valid key name | web | web |
Variable referenceDATA_DIR=$HOME/data | $HOME/data | $HOME/data (variables substituted) | $HOME/data |
KEY: valuePORT: 8080 | error: not a valid key name | 8080 | 8080 |
Key without =GIT_SHA | value from the shell, empty if unset | value from the shell, left out if unset | line ignored |
Also: kubectl rejects a repeated key, while Compose and dotenv keep the last one. All three skip blank lines and lines starting with #, and none of them minds a UTF-8 BOM at the start of the file.
Gotchas this tool checks for
Quotes and comments become part of the value
A file written for Docker Compose usually quotes values with spaces and adds comments after them. kubectl keeps both: CACHE_TTL=300 # seconds stores 300 # seconds, and your app reads that string. The differences list under the tool flags every such line.
One line per variable
kubectl reads the file line by line, so a quoted value that spans lines — a certificate, a JSON blob — breaks: the first line keeps an opening quote and the next line fails as a key. Put multi-line values in their own file and use --from-file instead.
Keys without a value come from the shell — even when unset
A line such as GIT_SHA makes kubectl call os.Getenv("GIT_SHA") in its own process. If the variable is not exported in that shell, kubectl does not skip the key: it stores an empty value, the same as for GIT_SHA=. Docker Compose leaves an unset key out instead. The value is used exactly as the shell holds it — line breaks, quotes, spaces and # included — and it is not limited to one line, since it never passes through the file reader.
When you paste env output, a value that spans several lines continues until a line that starts with NAME=; a continuation line that itself looks like NAME=… cannot be told apart, so check multi-line values. The command under Use it adds env -u KEY for keys you mark unset and never repeats a value you typed, so it stays out of your shell history.
A line of 64 KiB stops the file silently
kubectl's line reader gives up on a line of 65,536 bytes or more and never reports it: that key and everything after it are missing from the manifest, with no error. The tool warns when this happens.
Control characters
A ConfigMap value with a DEL or C1 control character makes kubectl fail with the error “yaml: control characters are not allowed”, and a NEL character is folded into a space. A Secret keeps both, because base64 carries the bytes.
A Secret is base64, not encryption
Anyone who can read the manifest can decode every value with one command. Kubernetes stores Secrets unencrypted in etcd unless encryption at rest is configured, and anyone allowed to create a Pod in the namespace can mount them.
Decode a Secret and see whyThe 1 MiB limit
The API server rejects a ConfigMap or Secret whose values add up to more than 1,048,576 bytes (1 MiB). The tool shows the total and warns above the limit; large files belong in a volume, not in an object.
Verification
- The reader is a port of
env_file.goink8s.io/kubectl(pkg/cmd/util) and of the key checks increate_configmap.goandcreate_secret.go, with the test vectors ofenv_file_test.go: BOM trimmed on the first line only, comments, blank lines, a key with an empty value. - The YAML is a port of the go-yaml v2 emitter kubectl prints with (via
sigs.k8s.io/yaml): key order, when a string is quoted, 80-column folding and escapes. - 89 env files, each converted to a ConfigMap and a Secret, were run through the real kubectl v1.37.2; the tool's output matches it byte for byte, including every error message. One finding: for keys such as
a10,a1banda09together, kubectl's own key order changes from run to run — the data does not. - 21 of those files have keys without
=, run with the variables set, set to an empty string and removed from kubectl's environment — multi-line, control-character and 70,000-byte values included. kubectl reads them withos.Getenv, so unset and empty give the same manifest; the tool's output matches every case. - Older kubectl releases also print
creationTimestamp: nullunder metadata; the data is identical. - Docker Compose rules follow
dotenv/parser.goin compose-go and the Docker documentation page on variable interpolation; dotenv rules follow the default parser in itslib/main.js. Sources read 2026-11-20. - The e2e spec proves the privacy claim: typing (shell values included), examples, copying and switching produce no request carrying input, the address bar never changes, and nothing is written to browser storage.