Picamera release resources before restarting the application


I don’t know if I’m asking a stupid question, but I’m having problems with the picamera in a Raspberry Pi 3.

In a microservices type application, I am using a container to read images from picamera. When I restart the application, sometimes the service which deals with the picamera does not release the resources, and after the restarting process it tries to create the picamera object again, which ends up in a “out of resources” exception.

Do you know if there is a way to shutdown gracefully the containers before restarting each service in the microservices application?

Thank you!