Python programs can crash with some uncaught exceptions (maybe due to poor coding, dynamic typing etc.).
I think if I spin up multiple instances of my app using Docker Swarm Mode (and use proper logging to know about the error and rectify it later) then if one instance crashes, my application will still be running (as I am using multiple instances). In the mean time, I can restart the crashed instance.
Please let me know if I have made any wrong assumptions here.
In FastAPI's Documentation, it mentions we can restart the program if crashed in Docker Swarm Mode. However, it does not explain how to do it (at least I could not find it).
Can anyone please let me know how to restart FastAPI automatically on crash using Docker in Swarm Mode.
Docker Swarm ensures that there are as many instances (containers) as you declare in your compose file.
For example, you can declare that you want to have six replicas
of FastAPI:
docker-compose.yml
services:
fast_api:
deploy:
mode: replicated
replicas: 6
If one of your (six) FastAPI containers crashes, the Swarm will automatically start a new one to ensure that, again, six containers are running.