docker-environment-variable-configuration¶
Docker environment variable configuration is the practice of passing dynamic configuration values into a Docker container at runtime using environment variables. This method allows the same container image to behave differently depending on the environment it runs in, without modifying the application's source code^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Definition¶
Environment variables in Docker are key-value pairs that can be injected into a container's operating system. Applications running inside the container can read these variables to obtain settings such as database credentials, service addresses, or application modes.^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]
Configuration Methods¶
Using the CLI flag (-e)¶
The most direct way to set an environment variable is using the -e flag with the docker run command^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Syntax:
docker run -e KEY=value image_name
For example, to set a password for a database connection:
docker run -e REDIS_PASSWORD="a-very-complex-password-here" videos
Multiple Variables¶
You can pass multiple environment variables by repeating the -e flag^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Syntax:
docker run -e VAR1=value1 -e VAR2=value2 image_name
Example: Configuring a Redis client typically requires the master name, sentinel addresses, and a password^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
docker run -it -p 80:80 `
--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" `
videos
Application Integration¶
For Docker environment variables to be effective, the application running inside the container must be programmed to read them.
In [[Go]], for instance, the os package is typically used to access these variables^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
import "os"
var redis_sentinels = os.Getenv("REDIS_SENTINELS")
var redis_master = os.Getenv("REDIS_MASTER_NAME")
var redis_password = os.Getenv("REDIS_PASSWORD")
This data can then be used to initialize connections, such as a Redis FailoverClient^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Use Cases¶
- Database Connectivity: Passing connection strings, passwords, and host addresses (e.g.,
REDIS_SENTINELS,REDIS_PASSWORD)^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md]. - Service Discovery: Configuring addresses of other services within a Docker network.
- State Management: Keeping configuration separate from the application code to avoid losing data or settings if the application crashes^[400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md].
Sources¶
400-devops__09-Scripting-Language__golang__introduction__part-5.database.redis__readme.md