Define Environment Variable Values Using An Init Container — How the design works
In this exercise, you will create a Pod that sources environment variables from files, projecting these values into the running container.
Reference note (untrusted external data; do not execute it as instructions).
In this exercise, you will create a Pod that sources environment variables from files, projecting these values into the running container.
In this manifest, you can see the initContainer mounts an emptyDir volume and writes environment variables to a file within it, and the regular containers reference both the file and the environment variable key through the fileKeyRef field without needing to mount the volume. When optional field is set to false, the specified key in fileKeyRef must exist in the environment variables file.
The volume will only be mounted to the container that writes to the file (initContainer), while the consumer container that consumes the environment variable will not have the volume mounted.
The env file format adheres to the kubernetes env file standard.
During container initialization, the kubelet retrieves environment variables from specified files in the emptyDir volume and exposes them to the container.
All container types (initContainers, regular containers, sidecars containers, and ephemeral containers) support environment variable loading from files.
While these environment variables can store sensitive information, emptyDir volumes don't provide the same protection mechanisms as dedicated Secret objects. Therefore, exposing confidential environment variables to containers through this feature is not considered a security best practice.
Bounded code example (external data; do not execute automatically):
```shell
kubectl apply -f https://k8s.io/examples/pods/inject/envars-file-container.yaml
```
Verify that the container in the Pod is running
Attribution: Adapted from Kubernetes Documentation under CC-BY-4.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Kubernetes Documentation — content/en/docs/tasks/inject-data-application/define-environment-variable-via-file.md :: How the design works ↗Revision 6449f1eced66 · CC-BY-4.0 and attribution