The yaml.scanner.ScannerError in Docker Compose occurs when the YAML parser encounters invalid syntax, usually due to tabs instead of spaces, incorrect indentation, or missing spaces after colons. This is one of the most common Docker Compose errors and is easily fixed by validating your YAML file.
The `yaml.scanner.ScannerError: while scanning a simple key` error occurs when Docker Compose's YAML parser encounters invalid syntax while reading your docker-compose.yml file. YAML is extremely sensitive to formatting, and this error typically indicates a structural problem with how your file is written. The "simple key" referenced in the error refers to YAML's internal concept of a key in a key-value pair. When the parser says it's having trouble "scanning" a simple key, it means it found unexpected characters or formatting where it expected to find a valid key definition. This error is thrown by the underlying PyYAML library that Docker Compose uses to parse configuration files. Unlike JSON, YAML relies on whitespace for structure, which makes it prone to subtle formatting errors that can be difficult to spot visually.
The error message includes the exact location of the problem. Look at the line and column numbers reported:
ERROR: yaml.scanner.ScannerError: while scanning a simple key
in "./docker-compose.yml", line 15, column 3Open your docker-compose.yml file and navigate to that specific line to inspect the formatting around it.
YAML does not allow tab characters for indentation. Convert all tabs to spaces (typically 2 spaces per indent level).
In VS Code:
1. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
2. Type "Convert Indentation to Spaces"
3. Select the option to convert
Using sed on Linux/Mac:
sed -i 's/ / /g' docker-compose.ymlVerify no tabs remain:
grep -P ' ' docker-compose.ymlIf this command returns nothing, your file has no tab characters.
Use an online YAML validator to identify syntax errors:
1. Go to [YAML Lint](https://www.yamllint.com/) or [Code Beautify YAML Validator](https://codebeautify.org/yaml-validator)
2. Paste your docker-compose.yml content
3. The validator will highlight exact syntax errors
Alternatively, use the docker compose config command to validate:
docker compose configThis parses the file and outputs the resolved configuration, or shows detailed error messages if parsing fails.
YAML requires a space after colons in key-value pairs. Check for lines like:
# Wrong - missing space after colon
restart:always
ports:
-"80:80"
# Correct - space after colon
restart: always
ports:
- "80:80"Pay special attention to any lines you recently edited.
Ensure consistent indentation throughout the file. All items at the same level must have identical indentation:
# Wrong - inconsistent indentation
services:
web:
image: nginx
ports: # Extra space causes error
- "80:80"
# Correct - consistent 2-space indentation
services:
web:
image: nginx
ports:
- "80:80"The services, volumes, and networks top-level keys should have no indentation. Their children should be indented by 2 spaces.
If you copied your docker-compose.yml from a PDF, Slack message, or website, hidden characters may have been introduced. The safest fix is to manually re-type the problematic section.
Alternatively, paste the content into a plain text editor first, then copy it again to your docker-compose.yml file.
Common sources of hidden characters:
- PDF documentation (often introduces special quote characters)
- Slack or Teams messages (may convert quotes to "smart quotes")
- Web pages with rich text formatting
Prevent future errors by installing a YAML linting extension in your editor:
VS Code:
- Install the "YAML" extension by Red Hat
- It provides real-time syntax highlighting and error detection
Enable the extension for Docker Compose files by adding to your VS Code settings:
{
"yaml.schemas": {
"https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json": "docker-compose*.yml"
}
}This provides autocomplete and validation specific to Docker Compose syntax.
### Understanding YAML Parsing Errors
The PyYAML library used by Docker Compose performs a two-phase parse: first scanning tokens, then parsing the structure. The "ScannerError" indicates failure in the first phase, meaning the raw text couldn't even be tokenized into valid YAML elements.
### Debugging Complex Files
For large docker-compose files, use a divide-and-conquer approach:
1. Comment out half the file
2. Run docker compose config
3. If it passes, the error is in the commented section
4. Repeat until you isolate the problem
### Multi-file Compose Setups
When using multiple compose files with -f flags, ensure each file is valid independently:
docker compose -f docker-compose.yml config
docker compose -f docker-compose.override.yml config### Environment Variable Syntax
Environment variables in Compose files have specific quoting rules:
# These are all valid
environment:
- DEBUG=true
- "PORT=8080"
- 'API_KEY=secret'
# This is problematic (nested quotes)
environment:
- API_URL="http://example.com" # The quotes become part of the valuedockerfile parse error line 5: unknown instruction: RRUN
How to fix 'unknown instruction' Dockerfile parse error in Docker
Error response from daemon: manifest for nginx:nonexistent not found: manifest unknown: manifest unknown
How to fix 'manifest for image:tag not found' in Docker
Error response from daemon: invalid reference format: repository name must be lowercase
How to fix 'repository name must be lowercase' in Docker
Error response from daemon: No such image
How to fix 'No such image' in Docker
Error response from daemon: Container is not running
How to fix 'Container is not running' when using docker exec