As you know we have lots of products and for some of them we use docker.
It turns out that it's very convenient to have multiple project specific .env files to define and/override different parameters.
This way we can have a configuration file that can be changes per project by defining .env.
Why do you need this?
In our SaaS apps we use different subfolders for subservices. Some of the projects can run on their own so it makes sense to have their own config files. For example we have a reverse proxy (nginx) which runs on higher than 1024 ports so it doesn't require more privileges. There are servers that will be running that container only so we could run it on the default ports 80 and 443.
In this case we want to override those ports per project or per server.
The following snippet scans up to 3 levels deep for .env files.
The files are read and the commented lines are skipped.
This is useful so you can turn on or off some variables or provide a description for a variable.
#!/bin/bash set -a # export all variables created next for proj_env_file in $(find . -maxdepth 3 -name '.env' -type f); do echo "Loading $proj_env_file" export $(echo $(cat $proj_env_file | sed 's/^[[:space:]]*#.*//g'| xargs) | envsubst) done
image credit: pexels pixabay #276452 on pexels