6.3.1.1 Environment Runtime Config
A focused guide to Environment Runtime Config, connecting core concepts with practical Docker and container operations.
Environment runtime config is the practice of using environment variables specifically as the mechanism for supplying configuration to a containerized application at run time, treating the container's environment as the primary interface between an application and the specific deployment context it happens to be running in.
Why Environment Variables Are a Natural Fit for This Role
Environment variables are universally supported across virtually every programming language and runtime, require no special file format or parsing library, and are naturally scoped to a single process — making them a convenient, broadly compatible mechanism for runtime configuration.
docker run -e DATABASE_URL=postgres://prod-db/app -e LOG_LEVEL=warn myapp:1.0
Reading Environment-Based Config Within an Application
An application reads its configuration directly from its own process environment, typically with sensible defaults for anything not explicitly supplied.
import os
database_url = os.environ.get("DATABASE_URL", "postgres://localhost/dev")
log_level = os.environ.get("LOG_LEVEL", "info")
Validating Required Configuration at Startup
An application can check for required configuration values immediately at startup, failing fast with a clear error rather than encountering a confusing failure later when that missing configuration is actually needed.
required_vars = ["DATABASE_URL", "SECRET_KEY"]
missing = [v for v in required_vars if v not in os.environ]
if missing:
raise RuntimeError(f"Missing required environment variables: {missing}")
Why This Approach Scales Well Across Environments
The same image, deployed across development, staging, and production environments, behaves correctly in each simply by varying its supplied environment variables, without needing any environment-specific image variant.
docker run --env-file staging.env myapp:1.0
docker run --env-file production.env myapp:1.0
Why Environment Runtime Config Matters
Treating environment variables as the primary runtime configuration interface is a widely adopted, broadly compatible practice (closely aligned with the well-known "twelve-factor app" methodology) that keeps a single image deployable correctly and consistently across many different environments.