go-yaml load error in parser (while parsing a block mapping) ... did not find
expected key means a line in your Compose file is indented differently from its
siblings, so the YAML parser cannot tell which mapping it belongs to. Line the keys up
with the same number of spaces.
The message gives a range, L4.C5-L8.C6: the mapping that started on line 4,
column 5, and the place the parser gave up, line 8, column 6. The broken line is usually
the last one in that range, or the one just above it.
The error
PS> docker compose config --quiet
go-yaml load error in parser (while parsing a block mapping) at L4.C5-L8.C6: did not find expected key
The file, where LANG has five spaces and GREETING six:
name: fix8-cli-c5
services:
web:
image: alpine
command: ["sleep", "1"]
environment:
GREETING: hello
LANG: C.UTF-8
Why it happens
YAML builds its structure from indentation alone. Keys of the same mapping must start in
the same column. LANG starts one column left of GREETING, so it
does not belong to environment; it is also one column right of
environment, so it does not belong to web either. The parser is
still inside the web mapping that began on line 4, expects the next key at
column 5, finds something at column 6, and stops. That is the range the message reports.
The usual sources are a pasted snippet with a different indent width, a tab mixed with
spaces, or a line edited by hand in a file that uses four-space indents elsewhere. Compose
fails before validation, so every command that reads the file, up included,
prints the same line.
The fix
name: fix8-cli-c5
services:
web:
image: alpine
command: ["sh", "-c", "echo $$GREETING $$LANG"]
environment:
GREETING: hello
LANG: C.UTF-8
PS> docker compose -f compose.fixed.yaml config --quiet
PS> docker compose -f compose.fixed.yaml up
web-1 | hello C.UTF-8
With both variables at six spaces, the file parses, config --quiet prints
nothing, and the container receives both values (the up output is trimmed to
the container's line). Turn on visible whitespace in your
editor when the column numbers in the message do not match what you see; a tab looks
like spaces but is not allowed for YAML indentation.
How it was reproduced
Docker Desktop on Windows 11, engine 29.9.2, Docker Compose v5.6.1, Windows PowerShell
5.1, the cached alpine:latest image. A one-service compose.yaml
with one environment key indented a single space short gave the message above on both
config and up; the aligned file ran and printed both values.
The project name fix8-cli-c5 is the test's own.
Frequently asked
- What does did not find expected key mean in docker compose?
- The YAML parser met a line whose indentation fits neither the current mapping nor its parent. Usually one key is a space short or long compared with its siblings.
- How do I read L4.C5-L8.C6 in a compose error?
- It is line and column: the mapping that started on line 4, column 5, and the point on line 8, column 6, where the parser stopped. Check the lines at the end of that range first.
- Can I use tabs in a docker compose file?
- No. YAML does not allow tabs for indentation. Use spaces, and keep keys of the same mapping in the same column.
More decoded errors in the Fixes category. A key that parses but is not allowed gives a different message, covered in services ... false schema.