Sub topic 9
Passing a variable from your shell
This chapter assumes you can run docker run, know what an environment variable is, and have used -e NAME=value to set one. Here you will learn the second form of -e, which takes a variable's value from your shell instead of writing it on the command line.
The name-only form of -e
If you give -e a name with no = and no value, Docker looks up that name in the environment of the shell that runs the command. It copies the current value into the container.
export DB_HOST=db.example.internal
docker run --rm -e DB_HOST alpine printenv DB_HOSTThe output is db.example.internal. The container never sees your whole environment. It only receives the variables you name.
The lookup happens in the Docker CLI on your machine, at the moment you run the command. The CLI reads the value and sends it to the Docker daemon as part of the container's configuration.
Why use it
Passing by name has three practical benefits:
- The value does not appear in the command text, so it stays out of your shell history and scripts.
- The same command works on every machine, and each machine supplies its own value.
- You can reuse one command in different environments, such as a laptop and a CI job, by changing the shell variable.
What happens when the variable is not set
If the name does not exist in your shell, Docker does not set it in the container. It does not set it to an empty string either. The variable is simply absent.
unset MISSING_VAR
docker run --rm -e MISSING_VAR alpine sh -c 'printenv MISSING_VAR; echo "exit code: $?"'printenv prints nothing and exits with code 1, because the variable does not exist in the container. Compare this with a variable that is set but empty:
export EMPTY_VAR=
docker run --rm -e EMPTY_VAR alpine sh -c 'printenv EMPTY_VAR; echo "exit code: $?"'Here printenv prints an empty line and exits with code 0. The variable exists, and its value is an empty string. Applications often treat "unset" and "empty" differently, so this distinction matters when you debug.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The variable is missing in the container | It was never exported or set in the shell that ran `docker run` | Set it first, then run the command again |
| The container has an old value | The container was created earlier, and its variables were fixed at creation | Create a new container with `docker run` |
| The value is set in the shell but not seen | You set it in a different terminal, or in a script that ran as a child process | Set it in the same shell session as `docker run` |
Variables are fixed when the container is created. Changing the variable in your shell later does not change a container that is already running.
Mixing forms in one command
You can combine the two forms. Each -e is independent.
export DB_HOST=db.example.internal
docker run --rm \
-e DB_HOST \
-e LOG_LEVEL=debug \
alpine sh -c 'printenv DB_HOST LOG_LEVEL'This prints both values, one per line. The first comes from your shell. The second is written in the command.
Exercise
You run unset API_URL and then docker run --rm -e API_URL alpine printenv API_URL. What does the container print, and what is the exit code of printenv?
Show answer
It prints nothing, and printenv exits with code 1. Because API_URL is not set in the shell, Docker does not define it in the container, so printenv cannot find it. If you had run export API_URL= first, the variable would exist with an empty value, and printenv would print an empty line and exit with code 0.
Exercise
You export DB_HOST=a.example, start a container with -e DB_HOST, and then run export DB_HOST=b.example in your shell. What value does the running container see?
Show answer
It still sees a.example. Docker read the value once, when the container was created. Changing the shell variable afterwards does not affect an existing container. To use b.example, start a new container.