It's better if the docker can recognize .env file in its root directory. Following is the working code in bin/moodle-docker-compose
# Load .env if it exists
if [ -f "$basedir/.env" ]; then
export $(grep -v '^#' "$basedir/.env" | xargs)
fi
It's very handy.