How to Shutdown Docker Stack on Container Failure
If you don't want the containers to restart, you can set the condition: to none. See https://docs.docker.com/compose/compose-file/#restart_policy

On the swarm manager, you could use docker stack ps to check the status of the containers on a regular basis and shut down the stack if the database containers aren't running (using cron or whatever). For example:

docker stack ps --filter "desired-state=Running" your-stack |grep database || docker stack rm your-stack.

If containers are not able to restart, they shouldn't show up in the list once they fail, and once none are found, you can remove the stack.