Suppose I m using a webserver / reverse proxy (traefik, nginx, apache, etc.) for various apps. If the webserver and all the apps are in the same docker-compose.yml
then it s easy to configure the network, e.g.:
networks:
traefik-appfoo:
traefik-appbar:
services
traefik:
# ...
networks:
- traefik-appfoo
- traefik-appbar
appfoo:
# ...
networks:
- traefik-appfoo
appbar:
# ...
networks:
- traefik-appbar
But that is a simplistic deployment. In reality I have an an ansible playbook with multiple roles:
roles/
traefik/ # installs and configures traefik
appfoo/ # installs and configures foo, which is reverse-proxied by traefik
appbar/ # installs and configures bar, which is reverse-proxied by traefik
appbaz/ # ...
playbook.yml
Each role has its own docker-compose.yml
. So I must somehow pass a network name from the app roles to the traefik role. The traefik role s docker-compose.yml
would look like this:
networks:
? # <----------
? # <----------
services
traefik:
# ...
networks:
- ? # <----------
- ? # <----------
I don t want to hardcode networks in the traefik role: that is messy, hard to maintain, and roles would not be independent.
In the past when I did this, in each app role I had ansible fragments which were uploaded to some directory on the server, e.g. traefik/docker-compose.yml.d/
. And every time I added a new app role, I d have to run a task to assemble the fragments into a complete docker-compose.yml
for traefik. If I had to run the original traefik role, it was idempotent as it would reassemble the fragments properly. This approach works, but it s very hard to maintain.
Is there a simpler way to handle this scenario? The same problem would apply for any app with dependent apps (that are installed later, in separate roles).