Skip to content
Blog

Setting Environment Variables at Runtime in Docker

Learn how to configure Docker containers dynamically at runtime using -e and --env flags, pass host variables, override Dockerfile defaults, and inspect runtime environment settings.

Published on • November 17, 2026

AI Assistant

Containerized applications require configuration management to run seamlessly across local development, staging, and production environments. Decoupling configuration settings from container images adheres to the Twelve-Factor App methodology.

Docker provides flexible mechanisms to pass environment variables into container runtimes dynamically using the -e and --env flags without rebuilding the underlying container image.


1. Setting Environment Variables with -e and --env

When starting a container with docker run or docker create, you can specify key-value environment variables directly on the command line:

docker run -d --name app-service \
  -e NODE_ENV=production \
  -e PORT=8080 \
  node:20-alpine node server.js

Both -e and --env syntax are functionally identical. Multiple variables can be defined by chaining the flag multiple times.


2. Passing Host Environment Variables Implicitly

Hardcoding secrets or variable values into command-line strings can expose sensitive data in shell history files (~/.bash_history or ~/.zsh_history).

To prevent shell leakage, Docker allows passing environment variables from the host terminal session by declaring only the key name without an equals sign (=):

# Export variable on host shell
export DATABASE_PASSWORD="SuperSecretHostPassword123!"

# Pass the host variable implicitly to the container
docker run -d --name secure-api \
  -e DATABASE_PASSWORD \
  my-api-image:latest

Docker reads DATABASE_PASSWORD from the active host shell environment and injects its value into the container runtime.


3. Overriding Dockerfile ENV Instructions

If a container image defines default environment variables inside its Dockerfile using the ENV instruction:

# Dockerfile snippet
FROM python:3.11-slim
ENV LOG_LEVEL="INFO"
ENV APP_TIMEOUT="30"

Passing -e or --env flags at runtime cleanly overrides the build-time ENV defaults:

# Override LOG_LEVEL to DEBUG at runtime
docker run -d --name debug-app \
  -e LOG_LEVEL="DEBUG" \
  python-app:v1

Runtime environment variables take immediate precedence over any ENV values compiled into the image layer manifest.


4. Inspecting Container Environment Variables

To confirm that environment variables have been properly injected into a running or stopped container, you can use docker inspect or docker exec.

Method A: Using docker inspect

The docker inspect command outputs detailed JSON metadata about the container configuration:

# Inspect container Environment array
docker inspect --format='{{json .Config.Env}}' debug-app

Output:

["LOG_LEVEL=DEBUG","APP_TIMEOUT=30","PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"]

Method B: Executing env Inside the Container

To check the live environment inside an active container, run the POSIX env or printenv utility via docker exec:

docker exec debug-app printenv LOG_LEVEL

5. Summary and Best Practices

Takeaway: Decouple application configuration from Docker images by using -e / --env flags at container startup. Pass sensitive credentials implicitly from host shell variables to avoid shell history logging, and verify runtime variable states using docker inspect or docker exec printenv.


Sources