You can use inheritance for this. If you have one “base” service where you set up the environment, all of your other services can inherit from that.
Example:
version: "2"
services:
base:
env_file:
- my_env.txt
web:
extends:
service: base
image: foo
database:
extends:
service: base
image: foo-db
The above example has everything in the same file, but you can also split this up into multiple files, where the base
service would reside in a base.yaml
file. You just need to add file: base.yaml
to the extends
section. Please see the documentation here.
I use this approach for setting the proxy variables for all containers. I have a proxy.yaml
file that defines a proxy-app
service that picks up the proxy environment variables from the shell. All of my real services extend the proxy-app
service and thus inherit the environment settings from that service.