Sub topic 10
Keeping variables in an --env-file
This chapter assumes you can run docker run, know what an environment variable is, and have used -e to set one. You will move a group of variables out of the command line and into a file.
Why use a file
Each -e flag adds one variable to the command. With five or ten variables, the command becomes long and hard to read. The same list is often needed in several commands, such as a test run and a debug run. The --env-file option reads variables from a file, so you write the list once and reuse it.
The file format
An env file is a plain text file with one NAME=value pair per line. Lines that start with # are comments and Docker ignores them. Blank lines are also ignored.
cat > app.env <<'EOF'
# application settings
GREETING=hello
LOG_LEVEL=debug
EOF
docker run --rm --env-file ./app.env alpine envThe env command prints every variable inside the container. You will see GREETING=hello and LOG_LEVEL=debug in the output, along with variables Docker adds itself, such as HOME and HOSTNAME.
Lines without a value
A line that contains only a name, with no =, works like -e NAME. Docker copies the value from the shell that runs the command. If that variable is not set in your shell, it is not set in the container either.
cat > pass.env <<'EOF'
# fixed value
LOG_LEVEL=info
# copied from the host shell
DB_HOST
EOF
export DB_HOST=db.example.internal
docker run --rm --env-file ./pass.env alpine envHow the pieces fit
Docker builds the container's environment from several sources. The image supplies defaults through ENV. The env file overrides those defaults. Each -e flag overrides the file.
You can combine both options in one command. If the same name appears in the file and in -e, the -e value is used.
docker run --rm --env-file ./app.env -e LOG_LEVEL=warn alpine envThe output shows LOG_LEVEL=warn and GREETING=hello.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `open ./app.env: no such file or directory` | The path is wrong. Paths are relative to the directory where you run the command. | Check the path with `ls`, or use an absolute path. |
| A value contains literal quote characters | Quotes were added in the file. | Remove the quotes. |
| A variable is missing in the container | A name-only line was used and the variable was not exported in the host shell. | Run `export NAME=value` first, or give the value in the file. |
| Password is visible in `docker inspect` | Env values are stored in the container's configuration. | See the chapter on why environment variables are a weak place for secrets. |
Keep the file out of version control
An env file often holds passwords and tokens. Do not commit it to Git. Add its name to .gitignore, and commit a template instead, for example app.env.example, which lists the names with placeholder values.
echo "app.env" >> .gitignoreKeeping values out of the command line also keeps them out of your shell history. The file still sits on disk in plain text, so restrict who can read it.
chmod 600 app.envExercise
A file named app.env contains LOG_LEVEL=debug and GREETING=hello. You run docker run --rm --env-file ./app.env -e LOG_LEVEL=warn alpine env. What are the values of LOG_LEVEL and GREETING in the container, and why?
Show answer
LOG_LEVEL is warn and GREETING is hello. A value set with -e overrides the same name in the env file, and variables that appear only in the file, such as GREETING, are still set.
Exercise
Your env file has the line API_URL="https://api.example.com". The application fails to connect. What is the likely problem, and how do you fix it?
Show answer
Docker does not strip quotes in an env file, so the value includes the quote characters and is not a valid URL. Change the line to API_URL=https://api.example.com.