How to DRY Docker Compose files

extends attribute in Docker Compose files allows to reuse existing service definitions for other services. That makes it possible to DRY Compose files.

name: foo

service:
  dev:
    image: ruby:3.3.5-alpine3.20
    environment:
      ENV: "development"
  test:
    extends:
      service: dev
    environment:
      ENV: "test"
compose.yml

In the definition above test service has exactly the same configuration dev and only redefines one environment variable. According to the documentation it’s also possible to include and reuse definitions from other files.


TIL rating:
good to know

Read next

env command allows to find a script’s interpreter when its location is a non-standard one. In scripts it’s recommended to use env in shebang lines to find the executable and run it.

#!/usr/bin/env ruby

puts "Hello, world."
Ruby interpreter is used without any option.

On GNU/Linux if it’s necessary to run a script’s interpreter with options, env has to be used with -S to pass options in shebang lines.