@AndMaz have you tried using the built-in healthcheck functionality for this kind of situation?
More info: Healthcheck implementation
Documentation: Compose specification | Docker Documentation
Example (rosetta-at-home): rosetta-at-home/docker-compose.yml at master · balena-labs-projects/rosetta-at-home · GitHub