Environment-based Configuration Pattern¶
The Environment-based Configuration Pattern is a software development practice where configuration settings—such as database connection strings, API keys, or service endpoints—are supplied to an application through the environment in which it runs, rather than being hardcoded into the source code^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
Purpose¶
This pattern is essential for maintaining the portability and security of an application^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]. By externalizing configuration, the same application artifact (e.g., a Docker container) can be deployed across different environments—such as development, testing, or production—without requiring code changes or rebuilds^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]. It also prevents sensitive information, like database passwords, from being exposed in version control^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
Implementation¶
Environment Variables¶
Configuration data is typically injected as environment variables. In a containerized context (e.g., Docker), these can be passed at runtime using the -e flag^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
For example, when connecting to a Redis cluster, configuration details such as the sentinel addresses, master name, and password are read from specific environment variables^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md].
import os
redis_sentinels = os.environ.get('REDIS_SENTINELS')
redis_master_name = os.environ.get('REDIS_MASTER_NAME')
redis_password = os.environ.get('REDIS_PASSWORD')
Runtime Injection¶
When launching a service, the required environment variables are mapped into the execution context^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]. A typical deployment command might look like the following:
docker run -it -p 5000:5000 \
--net redis \
-e REDIS_SENTINELS="sentinel-0:5000,sentinel-1:5000,sentinel-2:5000" \
-e REDIS_MASTER_NAME="mymaster" \
-e REDIS_PASSWORD="a-very-complex-password-here" \
customer-app
Related Concepts¶
- [[Twelve-Factor App methodology]]
- [[Containerization]]
- [[Deployment Automation]]
Sources¶
^[400-devops__09-Scripting-Language__python__introduction__part-5.database.redis__README.md]