Kubernetes equivalent of env-file in Docker
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Docker’s --env-file option lets you load environment variables from a simple key-value file. In Kubernetes, the closest equivalent is usually a ConfigMap or Secret combined with envFrom or individual env entries. The important nuance is that Kubernetes splits "non-sensitive config" and "sensitive config" into different resource types instead of treating everything as one plain file.
Use a ConfigMap for Non-Sensitive Variables
If your .env file contains ordinary configuration such as ports, hostnames, or feature flags, create a ConfigMap.
You can do that directly from an env-style file:
If .env contains:
then the ConfigMap stores those keys for later injection into pods.
Inject All Values with envFrom
The closest behavior to Docker --env-file is envFrom, which imports every key from the referenced ConfigMap.
That causes each key in app-env to become an environment variable inside the container.
This is the nearest Kubernetes equivalent to "load this env file into the container."
Use a Secret for Sensitive Values
If the file contains passwords, tokens, or API keys, use a Secret instead.
Then inject it the same way:
This keeps secret material in the resource type meant for sensitive configuration instead of mixing it with ordinary config.
Inject Specific Keys When You Need Control
Sometimes you do not want the whole file imported. In that case, map only the keys you need.
This is more verbose than envFrom, but it is better when you want tighter control over naming or exposure.
Do Not Confuse This with Docker Compose .env
There are two similar Docker concepts people often blur together:
- '
docker run --env-fileinjects environment variables' - Docker Compose
.envalso participates in variable substitution inside the YAML file
Kubernetes ConfigMap and Secret resources solve the injection side. Templating or substitution inside manifests is usually handled by tools such as Helm, Kustomize, or CI/CD pipelines rather than by Kubernetes itself.
That distinction matters. Kubernetes does not have one built-in feature that behaves exactly like every Docker .env use case.
Common Pitfalls
The first mistake is putting secrets into a ConfigMap. If the value is sensitive, use a Secret.
Another issue is expecting running pods to automatically refresh their environment variables after a ConfigMap or Secret change. Environment variables are set when the container starts, so pods usually need to be restarted or rolled out again.
Formatting problems also happen. --from-env-file expects simple KEY=VALUE lines. Shell expressions, export prefixes, and complex quoting rules from other tools do not always translate cleanly.
Finally, remember that envFrom imports everything. If the referenced resource contains more keys than the container should receive, use explicit env mappings instead.
Summary
- The Kubernetes equivalent of Docker
--env-fileis usually aConfigMaporSecretplusenvFrom. - Create a
ConfigMapwithkubectl create configmap --from-env-file. - Create a
Secretwithkubectl create secret generic --from-env-filefor sensitive data. - Use
envFromto import all keys, orenvto map selected keys. - Kubernetes config injection is not the same thing as Docker Compose
.envfile substitution.
Related reading
- Kubernetes error when forwarding a port - Connection refused
- Kubernetes executor do not parallelize sub DAGs execution in Airflow
- Kubernetes expired certificate
- Kubernetes ExternalName Service Add Headers
- Kubernetes Garbage Collection - no free space
- Kubernetes Helm, combine two variables with a string in the middle
- Kubernetes ExternalName Services
- Kubernetes Externalname working with https

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.