Extensions (original) (raw)

Extensions can be used to make your Compose file more efficient and easier to maintain.

Use the prefix x- as a top-level element to modularize configurations that you want to reuse. Compose ignores any fields that start with x-, this is the sole exception where Compose silently ignores unrecognized fields.

Extensions can also be used withanchors and aliases.

They also can be used within any structure in a Compose file where user-defined keys are not expected. Compose uses those to enable experimental features, the same way browsers add support forcustom CSS features

In this example, the environment variables do not belong to either of the services. They’ve been lifted out completely into the x-env extension field. This defines a new node which contains the environment field. The &env YAML anchor is used so both services can reference the extension field’s value as *env.

The nodeinfo and echoit services both include the x-function extension via the &function anchor, then set their specific image and environment.

UsingYAML merge it is also possible to use multiple extensions and share and override additional attributes for specific needs:

Note

YAML merge only applies to mappings, and can't be used with sequences.

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

This section is informative. At the time of writing, the following prefixes are known to exist:

Prefix Vendor/Organization
docker Docker
kubernetes Kubernetes

Values express a byte value as a string in {amount}{byte unit} format: The supported units are b (bytes), k or kb (kilo bytes), m or mb (mega bytes) and g or gb (giga bytes).

Values express a duration as a string in the form of {value}{unit}. The supported units are us (microseconds), ms (milliseconds), s (seconds), m (minutes) and h (hours). Values can combine multiple values without separator.