I'm trying to get started with Docker volumes. I have a simple Python Flask app that stores data in an sqlite3 data base. Running the app in a container without a volume works, but of course, every time I restart the container, all DB data is lost.
I've tried to read the available documentation on how to use volumes and I created a new, named volume:
docker volume create mydb
But I don't get what path I have to specify when starting the container with the volume. If I just do
docker run -p 5000:5000 -v mydb:/db my-app
it still loses all data when restarting. What do path do I need to specify instead of /db
? What does this depend on?
You can use bind mounts
or docker volumes
.
Assuming you selected /<path-to-db>
as the location of the database inside the container, and ./data
is the folder on your host filesystem you'd like to use to persist the database, or mydb
is the volume you use:
docker run -p 5000:5000 -v ./data:/<path-to-db> my-app
docker run -p 5000:5000 -v mydb:/<path-to-db> my-app
To find out which path inside the container you use to store the files (if you are not sure), look for sqlite3.connect
code: the argument is the path you need.