docker: --env-file: invalid env file (bad.env): variable 'APP MODE' contains
whitespaces means one line of the file you passed to docker run
--env-file has a space in the variable name, before the =. Replace the
space, with an underscore for example, and run the same command again.
The message names both the file and the variable, so the line is easy to find. The value
after the = was not the problem in the test; the name was.
The error
type bad.env
APP_NAME=clinic
APP MODE=demo
docker run --rm --env-file bad.env alpine printenv APP_MODE
docker: --env-file: invalid env file (bad.env): variable 'APP MODE' contains whitespaces
Run 'docker run --help' for more information
The exit code was 125. The first line of the file was valid, and the whole file was still
refused: printenv printed nothing.
Why it happens
An env file holds one NAME=value pair per line, and docker run
refused a line whose name had a space in it, naming the file and the variable. Here the
space was a typo for an underscore: the program read APP_MODE, and the file
said APP MODE.
The message starts with docker: --env-file: and has no Error response
from daemon part. For the same kind of mistake in a Compose .env file,
see the
Compose .env post; this one is about docker run only.
The fix
APP_NAME=clinic
APP_MODE=demo
docker run --rm --env-file good.env alpine printenv APP_MODE
demo
With the underscore in place the container started and printenv printed the
value from the file. If the name really should contain words, join them with underscores;
the program reading the variable has to use the same spelling. Spaces in the value were
accepted in two more one-line files: one inside the value, and one right after the
=, which the container received as part of the value:
type value.env
APP_NOTE=hello from the clinic
docker run --rm --env-file value.env alpine printenv APP_NOTE
hello from the clinic
type space.env
APP_NOTE= hello
docker run --rm --env-file space.env alpine sh -c "echo [$APP_NOTE]"
[ hello]
The brackets show the leading space kept in the value, so do not put a space after the
= unless the program expects one.
How it was reproduced
Docker Desktop 4.99.0 on Windows 11, client and engine 29.9.3, the cached
alpine:latest image (Alpine 3.24.2). The env files were plain ASCII written
for the test, two with two lines and two with one; the file and variable names are the
test's own. A PowerShell script ran each command through cmd /c with stderr
merged into stdout, which is why the files are shown with cmd's type and the
commands appear without a prompt. Runs: one with the bad file (the error), one with the
good file and two with the values that hold spaces.
Frequently asked
- What does variable contains whitespaces mean in docker run --env-file?
- A line in the env file has a space in the variable name, before the equals sign. The message names the file and the variable; replace the space with an underscore.
- Does one bad line stop the whole env file?
- Yes in the test. The first line was valid, but docker run refused the file, exited with 125 and printenv printed nothing.
- Can an env file value contain spaces in docker run?
- Yes in the test. APP_NOTE=hello from the clinic came through whole, and a space right after the equals sign was kept at the start of the value.
More decoded errors in the Fixes category.