Skip to content
Latchkey

Docker Compose "services.X.build must be a string or object" in Builds

Compose validated the build key of a service and found the wrong YAML type. build must be either a string (a context path) or a mapping (context, dockerfile, …) - not a list or scalar of another type.

What this error means

A docker compose build or up --build fails during config validation with services.<name>.build must be a string or object, before any image is built. The file otherwise looks reasonable.

docker compose output
services.api.build must be a string or object
# offending:
#   build:
#     - context: ./api      # a list, not a mapping

Common causes

build written as a list

A stray - turns the build mapping into a list (build:\n - context: ...). Compose expects either a bare path string or a key/value mapping, so a list is rejected.

Wrong indentation under the service

Misaligned indentation can make build’s children parse as something other than a mapping, or attach to the wrong service.

A scalar of the wrong shape

Setting build: to a number, boolean, or empty value (instead of a path string or mapping) fails the type check.

How to fix it

Use a valid string or mapping form

Either give a context path string, or a properly-indented mapping.

docker-compose.yml
services:
  api:
    build: ./api            # string form
  web:
    build:                  # mapping form
      context: ./web
      dockerfile: Dockerfile

Validate the compose config

Render the resolved config to catch the type error before building.

Terminal
docker compose config   # prints the parsed config or the validation error

How to prevent it

  • Run docker compose config in CI to validate before building.
  • Keep service keys consistently indented; avoid stray - under mappings.
  • Lint compose files with a YAML schema-aware linter.

Frequently asked questions

What causes ""build must be a string or object""?
A stray - turns the build mapping into a list (build:\n - context: ...). Compose expects either a bare path string or a key/value mapping, so a list is rejected.
How do I fix "build must be a string or object"?
Either give a context path string, or a properly-indented mapping.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card