> Markdown version of https://opspane.dev/kubernetes/env-to-configmap/ for AI agents and readers: the same text, without navigation or the interactive tool.
>
> The tool computes its result with JavaScript in the browser, so this file holds the reference text, worked examples and FAQ. Open the page to run it on your own input.
>
> The .env to ConfigMap & Secret converter takes no input from the URL on purpose: a .env file usually holds secrets, so it is pasted into the page and never leaves the tab. Its rules (kubectl --from-env-file keeps quotes, rejects `export`, errors on duplicate keys) are on the page and in its Markdown version.

# .env to ConfigMap & Secret Converter

Turn a .env file into the exact ConfigMap or Secret kubectl creates from it, with the same quotes, keys and errors, or a manifest back into .env.

## Quick answer

- kubectl's `--from-env-file` stores everything after the first `=` as written: `GREETING="hello"` becomes `"hello"`, quotes included.
- No `export` prefix: kubectl reads `export AWS_REGION` as the key and rejects it as not a valid key name.
- A repeated key is an error, where Docker Compose and dotenv let the last line win.
- A line with a key and no `=` takes its value from kubectl's shell, empty if unset. All values together may hold at most 1,048,576 bytes (1 MiB).

## How to convert a .env file to a ConfigMap or Secret

1. Paste your `.env` file, or pick an example. Choose ConfigMap or Secret, a name, and optionally a namespace and labels.
2. 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 of `env`, 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.
3. Read the manifest next to the file: it is the YAML that `kubectl create` with `--from-env-file` and `--dry-run=client -o yaml` prints, with the same key order and quoting. If kubectl would refuse the file, you see its exact error and the line.
4. Check _What kubectl stores_ and the Docker Compose / dotenv differences, then copy or download the manifest — or copy the command and run it yourself.
5. 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 value `GREETING="hello world"` | `"hello world"` | `hello world` | `hello world` |
| Single quotes `TOKEN='abc 123'` | `'abc 123'` | `abc 123` | `abc 123` |
| Escape sequences `MESSAGE="line 1\nline 2"` | `"line 1\nline 2"` | `line 1↵line 2` | `line 1↵line 2` |
| Inline comment after a space `TIMEOUT=30 # seconds` | `30 # seconds` | `30` | `30` |
| # inside a value `URL=https://example.test/#top` | `https://example.test/#top` | `https://example.test/#top` | `https://example.test/` |
| export prefix `export 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 reference `DATA_DIR=$HOME/data` | `$HOME/data` | `$HOME/data (variables substituted)` | `$HOME/data` |
| KEY: value `PORT: 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 why](https://opspane.dev/kubernetes/secret-decoder/)

### The 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.go` in `k8s.io/kubectl` (`pkg/cmd/util`) and of the key checks in `create_configmap.go` and `create_secret.go`, with the test vectors of `env_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`, `a1b` and `a09` together, 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 with `os.Getenv`, so unset and empty give the same manifest; the tool's output matches every case.
- Older kubectl releases also print `creationTimestamp: null` under metadata; the data is identical.
- Docker Compose rules follow `dotenv/parser.go` in compose-go and the Docker documentation page on variable interpolation; dotenv rules follow the default parser in its `lib/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.

## FAQ

### Does kubectl --from-env-file remove quotes from values?

No. kubectl stores everything after the first "=" exactly as written, so GREETING="hello" becomes the 7-character value "hello", quotes included. Docker Compose and the dotenv package strip them, which is why the same file can behave differently in a container started by Compose and in a Pod.

### Why does kubectl say "export AWS_REGION" is not a valid key name?

kubectl does not understand the shell export prefix, so the key becomes "export AWS_REGION" — with a space — and fails the environment-variable name check: error: "export AWS_REGION" is not a valid key name: a valid environment variable name must consist of alphabetic charact… Remove "export " from the file; Compose and dotenv strip it silently.

### What happens when a key appears twice?

kubectl stops with an error instead of keeping the last value: error: cannot add key "PORT", another key by that name already exists in Data for ConfigMap "app-config". Docker Compose and dotenv let the later line win, so a file that works for them can fail here.

### What does a line with only a key, and no "=", do?

kubectl reads that variable from the environment of the shell running it, with Go's os.Getenv. An unset variable and an empty one look the same to it, so an unset GIT_SHA still becomes a key with an empty value (GIT_SHA: ""); Docker Compose would leave the key out. Enter the value your shell holds under Values from your shell, paste the output of env, or mark the key unset, and the manifest shows exactly what kubectl would store.

### Why are some values quoted in the YAML?

go-yaml quotes a string that would otherwise read back as a number, a boolean or null, so it stays a string: DEBUG: "false", LOG_LEVEL: info, PORT: "8080", name: app-config. The value is the same text in every case; ConfigMap data only holds strings.

### Should a Secret use data or stringData?

kubectl create secret writes data, with each value base64-encoded (hunter2 becomes aHVudGVyMg==). stringData takes plaintext and the API server encodes it on write, which reads better in review but is not what kubectl prints. Both are stored the same way.

### Is a Secret encrypted?

No — base64 is an encoding, reversible by anyone who can read the manifest. Kubernetes documents that Secrets are stored unencrypted in etcd by default; protection comes from encryption at rest and RBAC. The Secret decoder page shows the round trip.

### Can I turn a ConfigMap or Secret back into a .env file?

Yes: switch to manifest → .env and paste the YAML. Secret values are decoded in the tab. Values an env file cannot hold — line breaks, binary data, keys that are not valid variable names — are left out and listed, so the file you get reads back into the same data.

## Related tools

- [Kubernetes Secret Decoder](https://opspane.dev/kubernetes/secret-decoder/): Decode every value in a Kubernetes Secret from pasted YAML and encode data back into a manifest. Nothing leaves your browser.
- [Kubernetes Resource Units Converter](https://opspane.dev/kubernetes/resource-units/): Convert Kubernetes CPU and memory quantities: millicores, Mi vs M, the form kubectl shows, and the mistakes that break a limit.
- [Kubernetes CronJob Generator](https://opspane.dev/cron/kubernetes/): Generate a complete CronJob manifest with schedule, timeZone, concurrencyPolicy and history limits, explained field by field.
- [Kubernetes QoS Class & Requests Checker](https://opspane.dev/kubernetes/requests-limits/): Paste a Pod or Deployment: get its QoS class and why, the Pod request with init and sidecar containers, oom_score_adj and how many Pods fit a node.
