failed to read ...\.env: line 2: key cannot contain a space — a line in the .env file next to your compose.yaml has a space before its =, or is plain text with no = at all. Fix or comment out the line the message names.

Write keys without spaces (DB_HOST=db) and start notes with #. The line number in the message is the line to look at.

The error

PS> docker compose -p fix10-cs-q5 config
failed to read C:\...\l2\q5b\.env: line 2: key cannot contain a space

The command exited with code 1. The path to the test folder is shortened; nothing else in the line was changed. The .env file:

APP_NAME=clinic-demo
DB HOST=db

Why it happens

Compose loads the .env file in the project folder on its own, and here it stopped on line 2 of it: the part before =, DB HOST, has a space. The Compose file did not even use DB_HOST; one bad line in .env was enough.

A note written as plain text, with spaces and no =, gave the very same message:

APP_NAME=clinic-demo
remember to set the database password
PS> docker compose -p fix10-cs-q5 config
failed to read C:\...\l2\q5c\.env: line 2: key cannot contain a space

A dash in the key, DB-HOST=db, did not give an error in the same test: config exited with code 0 and up ran the service.

The fix

APP_NAME=clinic-demo
DB_HOST=db
# remember to set the database password
PS> docker compose -p fix10-cs-q5f up
 Network fix10-cs-q5f_default Creating
...
 Container fix10-cs-q5f-web-1 Created
Attaching to web-1
 Container fix10-cs-q5f-web-1 Starting
web-1  | app name is clinic-demo
...

With the space replaced by an underscore and the note turned into a # comment, the file was read: under a project name not used before, Compose created a new network and container, and the service printed the value of APP_NAME from the fixed file. The message gives the full path of the .env file it read, which tells you which folder's file is at fault when you keep several projects side by side.

How it was reproduced

One alpine service whose environment used ${APP_NAME}, with four .env files next to it in turn: one with DB-HOST=db (no error), one with DB HOST=db, one with a line of plain text, and the corrected one. Each was checked with docker compose config or started with docker compose up from Windows PowerShell 5.1. Docker Compose v5.7.0, Docker Engine 29.9.3, Docker Desktop 4.99.0 on Windows (version 10.0.26300). The project names fix10-cs-q5 and fix10-cs-q5f (the final run of the corrected file) and the values are this test's own; each was removed with docker compose down -v.

Frequently asked

What does 'key cannot contain a space' mean in docker compose?
A line in the .env file has a space in the part before the equals sign, or is plain text with no equals sign. Compose names the file and the line number.
How do I write a comment in a docker compose .env file?
Start the line with #. In this test a note written as plain text gave the key cannot contain a space error, and the same note after # was accepted.
Can a .env key contain a dash for docker compose?
In this test DB-HOST=db was accepted: config exited with code 0 and up ran the service. A space in the key was refused.

More decoded errors in the Fixes category. When the file is fine but a variable has no value, see Docker Compose: variable is not set.