Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 1.07 KB

10-fragments.md

File metadata and controls

35 lines (28 loc) · 1.07 KB

Fragments

It is possible to re-use configuration fragments using YAML anchors.

volumes:
  db-data: &default-volume
    driver: default
  metrics: *default-volume

In previous sample, an anchor is created as default-volume based on db-data volume specification. It is later reused by alias *default-volume to define metrics volume. Same logic can apply to any element in a Compose file. Anchor resolution MUST take place before variables interpolation, so variables can't be used to set anchors or aliases.

It is also possible to partially override values set by anchor reference using the YAML merge type. In following example, metrics volume specification uses alias to avoid repetition but override name attribute:

services:
  backend:
    image: awesome/database
    volumes:
      - db-data
      - metrics
volumes:
  db-data: &default-volume
    driver: default
    name: "data"
  metrics:
    <<: *default-volume
    name: "metrics"