Skip to main content
In this lesson you’ll learn how to set environment variables for containers running in Kubernetes and how that maps from the typical Docker -e pattern.
A presentation slide titled "Environment Variables" with a code/HTML icon on a dark blue curved panel to the right. The slide has a clean white background and a small "Copyright KodeKloud" note in the corner.

Quick comparison: Docker vs Kubernetes

When running a container locally with Docker you can pass environment variables using -e:
In Kubernetes, you configure environment variables inside the container spec of a Pod, Deployment, or other workload manifest using the env (or envFrom) fields.
Use env for individual variables (static or referenced), and envFrom to import all keys from a ConfigMap or Secret. The same env configuration works inside any workload that has a containers spec (Pod, Deployment, ReplicaSet, etc.).

Basic example — key/value in a Pod manifest

Add an env array to the container spec. Each item uses name and value. This example shows a direct, static environment variable:
Notes:
  • env is an array, so each environment variable is prefixed with -.
  • The same env entries can be put into a containers entry inside a Deployment manifest.

Referencing ConfigMaps and Secrets

Instead of hardcoding values, reference keys from a ConfigMap or Secret using valueFrom. This is the preferred pattern for separating configuration and secrets from container images. Example referencing a key from a ConfigMap:
Example referencing a key from a Secret:

Importing all keys with envFrom

If you want to expose every key from a ConfigMap or Secret as environment variables, use envFrom. This will create one environment variable per key in the referenced object.

When to use each approach

Security reminder

Secrets in Kubernetes are base64-encoded, not encrypted by default (see Kubernetes documentation: https://kubernetes.io/docs/concepts/configuration/secret/). Treat them as sensitive and consider using appropriate secrets management (encryption at rest, RBAC controls, or external secret stores) for production workloads.
That’s it — you’ve seen the common patterns to inject environment variables into containers in Kubernetes and when to use static values, ConfigMaps, Secrets, or envFrom.

Watch Video