basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    Write shared service fields once: x- extensions and YAML anchors

    Logging, restart, and security_opt repeat in every service, and an edit to one gets missed in another. Put the shared block in x-defaults with an &defaults anchor, and pull it into each service with <<: *defaults.

    Why

    Three services, each carrying the same restart, security_opt, and log rotation block:

    services:
      app:
        image: ghcr.io/example-user/app:latest
        restart: unless-stopped
        security_opt: ["no-new-privileges:true"]
        logging:
          driver: json-file
          options:
            max-size: 10m
            max-file: "3"
      cache:
        image: redis:7.4-alpine
        restart: unless-stopped
        security_opt: ["no-new-privileges:true"]
        logging:
          driver: json-file
          options:
            max-size: 10m
            max-file: "3"
      db:
        image: mariadb:11.8
        restart: always
        security_opt: ["no-new-privileges:true"]
        logging:
          driver: json-file
          options:
            max-size: 10m
            max-file: "3"

    Seven lines repeated three times, with only restart differing on db. Changing max-size means editing three places without missing one. Add a fourth service without logging and its log grows without limit: Docker doesn’t rotate logs by default, so the file keeps growing until it fills the disk.

    How to write it

    Define the shared fields once at the top of the file and pull them into each service with one line:

    x-defaults: &defaults
      restart: unless-stopped
      security_opt: ["no-new-privileges:true"]
      logging:
        driver: json-file
        options:
          max-size: 10m
          max-file: "3"
    
    services:
      app:
        <<: *defaults
        image: ghcr.io/example-user/app:latest
      cache:
        <<: *defaults
        image: redis:7.4-alpine
      db:
        <<: *defaults
        image: mariadb:11.8
        restart: always

    &defaults names the block, *defaults references it by name, and <<: inserts all of its keys into the service as if you had written them there by hand. If the service defines the same key itself, the service wins: db keeps restart: always.

    Compose ignores top-level keys starting with x-, so that’s where reusable templates belong. Without x-, Compose rejects the unknown key (first case below).

    A service’s environment replaces the shared one

    <<: inserts the anchor’s keys as-is and does not merge nested mappings. If a service defines its own environment:, it replaces the anchor’s environment: rather than appending to it. To add per-service variables on top of shared ones, merge the anchor inside environment: itself:

    x-env: &env
      TZ: Etc/UTC
    
    services:
      worker:
        <<: *defaults
        environment:
          <<: *env
          QUEUE: mail

    worker gets both TZ and QUEUE. Lists (security_opt, ports) can’t be merged this way: a list on the service replaces the list from the anchor in full.

    If it doesn’t work

    validating …/compose.yaml: additional properties 'defaults' not allowed

    Cause
    The shared block name doesn't start with x-. Compose accepts only its own top-level keys and ignores anything prefixed with x-.
    Fix
    Rename it to x-defaults.

    unknown anchor 'defaults' referenced when running with compose.override.yaml

    Cause
    An anchor is visible only inside its own file. Override files and files loaded via include: can't see anchors from the main file.
    Fix
    Repeat the block in the override or define a separate anchor there.

    A service lost a variable from the shared block

    Cause
    The service defines its own environment:, which replaced the entire environment: from the anchor. <<: merges keys as-is and doesn't merge nested blocks.
    Fix
    Pull the shared block in inside environment:: put <<: *env on the first line and the service's own variables below it.

    How to check

    docker compose config prints the file with anchors expanded: each service should show the full block.

    docker compose config | grep -A4 'logging:'