Setting variable value once in compose for all services


I am trying to understand if there is a way to set the value of an environment variable at the top level of a compose file and use it in services. I saw that it is possible to set a value via the per service environment tag. I tried to put that at the top-level but docker-compose complained tha this is invalid.

I understand that it is possible to use a .env file but I would rather set that once at the beginning of the compose file. Is there a method to achieve this?

I feel like you refer to extension fields:

I hate .env files - a second file to maintain and even worst: they don’t work for swarm stacks.

No, extension is not what I am looking for. I asked the question in a very generic way but I fact my first use case if to be abel to prepend or postpend the image value. May be to specify a different repo, or version, etc.