Home Explore Blog CI



docker

content/reference/compose-file/fragments.md
2147b3dc065a0b0bad4eef20caebc4af4e96ba671dbbad7e00000003000009b8
---
title: Fragments
description: Understand how to use fragments
keywords: compose, compose specification, fragments, compose file reference
aliases: 
 - /compose/compose-file/10-fragments/
weight: 70
---

{{% include "compose/fragments.md" %}}

Anchors are created using the `&` sign. The sign is followed by an alias name. You can use this alias with the `*` sign later to reference the value following the anchor. Make sure there is no space between the `&` and the `*` characters and the following alias name. 

You can use more than one anchor and alias in a single Compose file.

## Example 1

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

In the example above, a `default-volume` anchor is created based on the `db-data` volume. It is later reused by the alias `*default-volume` to define the `metrics` volume. 

Anchor resolution takes place before [variables interpolation](interpolation.md), so variables can't be used to set anchors or aliases.

## Example 2

```yml
services:
  first:
    image: my-image:latest
    environment: &env
      - CONFIG_KEY
      - EXAMPLE_KEY
      - DEMO_VAR
  second:
    image: another-image:latest
    environment: *env
```

If you have an anchor that you want to use in more than one service, use it in conjunction with an [extension](extension.md) to make your Compose file easier to maintain.

## Example 3

You may want to partially override values. Compose follows the rule outlined by [YAML merge type](https://yaml.org/type/merge.html). 

In the following example, `metrics` volume specification uses alias
to avoid repetition but overrides `name` attribute:

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

## Example 4

You can also extend the anchor to add additional values.

```yml
services:
  first:
    image: my-image:latest
    environment: &amp;env
      FOO: BAR
      ZOT: QUIX
  second:
    image: another-image:latest
    environment:
      <<: *env
      YET_ANOTHER: VARIABLE
```

> [!NOTE]
>
> [YAML merge](https://yaml.org/type/merge.html) only applies to mappings, and can't be used with sequences. 

In example above, the environment variables must be declared using the `FOO: BAR` mapping syntax, while the sequence syntax `- FOO=BAR` is only valid when no fragments are involved. 

Chunks
c7453e18 (1st chunk of `content/reference/compose-file/fragments.md`)
Title: Compose File Fragments (Anchors and Aliases)
Summary
This section explains how to use YAML anchors and aliases in Docker Compose files to avoid repetition and improve maintainability. Anchors are defined using `&` and referenced using `*`. The document provides examples of reusing volume definitions and environment variables across services, as well as overriding and extending anchored values. It also highlights the limitations of YAML merge, noting that it only applies to mappings, not sequences.