Sub topic 11
How -e Interacts with ENV in the Image
This chapter assumes you can build a small image from a Dockerfile, run it with docker run, and set a variable with -e. You should also know what an environment variable is: a name and value pair that a process reads at start-up.
Two places a variable can be set
A variable can come from two places:
- The image. A Dockerfile
ENVinstruction stores a default value in the image metadata. - The run command. The
-eand--env-fileflags supply a value when the container starts.
When both set the same name, the run-time value wins. The ENV value acts only as a default.
A worked example
Create a Dockerfile that sets a default and prints it.
FROM alpine
ENV APP_MODE=production
CMD ["printenv", "APP_MODE"]Build the image and run it twice.
docker build -t envdemo .
docker run --rm envdemo
docker run --rm -e APP_MODE=development envdemoThe first run prints production, which is the image default. The second prints development, because -e replaced the default for that one container.
The override applies only to the container you start. The image is unchanged, so the next run without -e prints production again.
Variables that exist only in the image
The override rule also works in the other direction. Variables you do not pass at run time still reach the container if the image defines them. You can see the combined result by listing the environment:
docker run --rm -e LOG_LEVEL=debug envdemo envThe output contains APP_MODE=production from the image and LOG_LEVEL=debug from the command, along with a few variables Docker and the shell add, such as PATH and HOSTNAME. Here the command env replaces the image's CMD, which is why the output lists everything instead of only APP_MODE.
Precedence among run-time sources
You can combine -e and --env-file in one command. If both define the same name, the -e flag takes precedence over the file. Use this when a file holds shared settings and you need to change one value for a single run.
printf 'APP_MODE=staging\n' > app.env
docker run --rm --env-file ./app.env -e APP_MODE=development envdemoThis prints development. Without the -e flag it would print staging, and without either it would print production.
Common errors
- Expecting a rebuild. Changing a value with
-edoes not change the image. If you want a new default, edit theENVline and rebuild. - Setting an empty value.
-e APP_MODE=sets the variable to an empty string, which still overrides the image default. If your application treats empty as "missing", it may fall back to its own default instead of the one in the image. - Misspelling the name. Variable names are case-sensitive.
-e app_mode=developmentcreates a second variable and leavesAPP_MODEatproduction. Check withdocker run --rm -e app_mode=development envdemo env. - Secrets baked into
ENV. Any value set withENVis stored in the image and appears indocker image inspectanddocker history. Anyone who can pull the image can read it.
Checking what a container received
To see the environment Docker configured for a running container, inspect it:
docker run -d --name envcheck -e APP_MODE=development envdemo sleep 60
docker inspect --format '{{json .Config.Env}}' envcheck
docker rm -f envcheckThe list shows the merged result, with the run-time value for APP_MODE rather than the image default.
Exercise
An image sets ENV APP_MODE=production. A file app.env contains APP_MODE=staging. You run docker run --rm --env-file ./app.env -e APP_MODE=test envdemo. What does printenv APP_MODE print inside the container, and why?
Show answer
It prints test. Run-time values override the image's ENV default, and among run-time sources the -e flag takes precedence over --env-file. The values production and staging are both replaced.
Exercise
You run docker run --rm -e APP_MODE= envdemo. What does the container print, and how does this differ from not passing -e at all?
Show answer
It prints an empty line. -e APP_MODE= sets the variable to an empty string, which overrides the image default of production. Without -e, the container would print production.