Docker Compose offers couple of ways to utilize code re-usability through out your YAML file.
How to re-use
version: '3.7' # Templates: x-base: &base-service-template init: true build: context: . env_file: - .env networks: - local x-productservice: $product-service-template env_file: - .env - ./ProductService/product_env services: userservice: <<: *base-service-template image: userservice.image container_name: userservice.container build: dockerfile: ./UserService/Dockerfile ports: - "5001:80" productservice: <<: *base-service-template <<: *product-service-template image: productservice.image container_name: productservice.container build: dockerfile: ./ProductService/Dockerfile env_file: - ./ProductService/product_env ports: - "5651:80" networks: local: name: local
Maybe it looks a bit weird, but when you take in consideration how much configuration items one can actually reuse this way,
not to mention how much easier is to maintain them there is not much doubt left regarding the usefulness of extension field.
One should also keep an eye on docker template, which are currently an experimental feature. The template resembles the way k8s deals with YAML manifest for its different resource types, which IMO is a good thing.