An environment variable is a named key/value setting supplied to a process by its execution environment. An application can read one to choose a URL, logging level, or feature setting without changing its code. Docker supplies variables to containers: an image can define defaults, and you can provide different values when creating a container. section 3.1
Change a container without changing its image
Run `docker container run --env TARGET=google.com diamol/ch03-web-ping:2e`. The web-ping application now sends requests to google.com instead of its default target. The same image supports both behaviors; end this interactive run with Ctrl-C. section 3.1
Choose where each setting belongs:
- Set an image default with Dockerfile `ENV`, such as `ENV TARGET="blog.sixeyed.com"`. Override it when you create a container with `--env` or `-e`. section 3.2 section 3.1
- For a Compose service, use `environment` for a setting or `env_file` to load settings from a file. Compose files can also substitute a variable such as `${TODO_WEB_PORT}` into a published-port setting. section 10.3
- For a reusable image, combine packaged defaults with environment-specific configuration files, then use variables for targeted overrides. The application must be set up to merge those sources. section 18.0 section 18.1
Listing 10.4 The override adds configuration and a secret
services:
todo-web:
ports:
- 8089:8080
environment:
- Database:Provider=Sqlite
env_file:
- ./config/logging.debug.env
secrets:
todo-db-connection:
file: ./config/empty.json
This Compose override shows a setting under `environment`, an `env_file` for logging settings, and a separate file-backed secret. section 10.3
Check the result rather than assuming an override took effect. For web-ping, compare its request output before and after changing `TARGET`. Other applications expose configuration through an API or show its effects in logs. If an older application cannot read environment variables, a startup utility can merge variable and file overrides and write the configuration file it expects. section 3.1 section 18.4
The production payoff is one image promoted through test and production with different settings supplied at deployment. Variables are especially useful for quick feature-level changes, while environment-level overrides commonly come from files. section 18.5


















