Sub topic 8
Setting environment variables with -e
This chapter assumes you can run docker run, know what an image and a container are, and are comfortable in a shell. It covers the -e flag: how to set a variable, how to copy one from your shell, and what happens when the image already defines the same name.
What -e does
An environment variable is a name and value pair that a process can read when it starts. Many images read their settings this way, such as a log level, a database host, or a password. The -e flag (long form --env) sets a variable for the container's main process.
docker run --rm -e GREETING=hello alpine printenv GREETINGThis prints hello. The value reached the process inside the container, which is what printenv shows.
You can repeat the flag to set several variables:
docker run --rm -e GREETING=hello -e LOG_LEVEL=debug alpine envQuote values that contain spaces or characters the shell treats specially, for example -e "MESSAGE=hello world".
Passing a variable from your shell
If you give -e only a name, Docker copies the current value from the shell that runs the command.
export DB_HOST=db.example.internal
docker run --rm -e DB_HOST alpine printenv DB_HOSTThe output is db.example.internal. This keeps the value out of the command text, so it does not appear in your shell history or in scripts. If the variable is not set in your shell, Docker does not define it in the container at all. It is not set to an empty string.
Keeping variables in an --env-file
When you have many variables, put them in a file. Each line is VAR=value. Lines that begin with # are ignored.
cat > app.env <<'EOF'
# application settings
GREETING=hello
LOG_LEVEL=debug
EOF
docker run --rm --env-file ./app.env alpine envThe output includes GREETING=hello and LOG_LEVEL=debug. The file can be reused across commands and kept out of version control.
A few details apply to env files:
- Values are taken literally. Docker does not remove quotation marks, so
NAME="x"gives a value that includes the quote characters. - A line with only a name, such as
DB_HOST, copies the value from your shell, like-e DB_HOST. - You can combine
--env-fileand-ein one command.
How -e interacts with ENV in the image
A Dockerfile can set a default with ENV. A value given at run time replaces it. This image sets a default and prints it:
FROM alpine
ENV APP_MODE=production
CMD ["printenv", "APP_MODE"]Build it and run it twice:
docker build -t envdemo .
docker run --rm envdemo
docker run --rm -e APP_MODE=development envdemoThe first run prints production, the image default. The second prints development, because -e overrides ENV. The image is unchanged. The override applies only to that container.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The variable is missing in the container | `-e NAME` was used but `NAME` is not exported in your shell | Run `export NAME=value` first, or give the value explicitly |
| The value contains literal quote characters | Quotes in an `--env-file` are not stripped | Remove the quotes in the file |
| The container still uses the old value | The flag was placed after the image name | Put all `docker run` options before the image name |
| The variable has no effect | The application does not read that variable | Check the image's documentation for the names it supports |
The third row catches many people. Anything after the image name is passed to the container as the command and its arguments, not to Docker.
Exercise
An image sets ENV LOG_LEVEL=info. You run docker run --rm -e LOG_LEVEL=debug myimage. What value does the container see, and does the image change?
Show answer
The container sees debug. The -e value overrides the image's ENV default for that container only. The image itself is not changed, so the next run without -e uses info again.
Exercise
You run docker run --rm alpine printenv API_URL -e API_URL=http://x. Why does this not behave as intended?
Show answer
The -e flag comes after the image name, so Docker passes it to the container as part of the command rather than reading it as an option. Move -e API_URL=http://x before alpine.