Most images read their settings from environment variables. You set them when the container is created.
-e: one variable
docker run -d --name hello -p 8080:8080 -e GREETING=Hello -e NAME=ops stackmonsters/greeter:1.0
curl -s localhost:8080Hello, ops!| Form | Value in the container |
|---|---|
-e GREETING=Hello | Hello |
-e GREETING="Hello there" | Hello there (your shell removes the quotes) |
-e NAME | The value of NAME in your shell; unset if your shell has none |
Required variables
Many images stop at startup when a variable is missing. The reason is in the logs:
docker run -d --name greet stackmonsters/greeter:1.0
docker logs greetFATAL: GREETING is not setdocker run -d --name db postgres:16
docker logs dbError: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".--env-file: many variables
Keep the settings in a file, one KEY=value per line:
# greeter settings for the team page
GREETING=Welcome
NAME=platform teamdocker run -d --name team -p 8081:8080 --env-file ~/team.env stackmonsters/greeter:1.0
curl -s localhost:8081Welcome, platform team!File rules:
| Line | Effect |
|---|---|
# comment, blank line | Ignored |
NAME=platform team | Value is platform team, spaces included |
GREETING="Hi" | Value is "Hi", quotes included (no shell parsing) |
NAME | Copied from your shell, like -e NAME |
Where a variable comes from
image ENV ──► --env-file ──► -e later sources override earlier onesThe greeter image already sets PORT=8080. List what a running container sees:
docker exec hello env
HOME=/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PORT=8080
GREETING=Hello
NAME=ops
HOSTNAME=712ce5f8c2ed
...PATH and PORT come from the image. GREETING and NAME come from -e. Setting -e PORT=9090 would move the greeter to port 9090, and -p would then need :9090 on the right.
Variables are fixed at creation
Like ports, the environment is stored in the container's configuration. Editing the env file or your shell does nothing to an existing container, and docker restart reuses the old values. Remove the container and create it again.