How can I persist agent status for docker based machine

I have installed the enterprise version using docker-compose. I am running an R script in a notebook. This R script depends on many libraries, which take around 40+ minutes to install. However, whenever I restart the docker machine or restart the host machine (an EC2 server), I lose all my installations, and I need to reinstall them. Is there a way to persist the state of the docker machine and use the same docker machine every time for this notebook?