Quick refresh on one way to manage a Mongo database for access by Python programs.
Set a directory to store the database. Let’s call this
Then run a docker container with the most recent version of MongoDB:
docker run --name mongoserver -p 27017:27017 -v /mnt/mongodata:/data/db mongo.
Just for completeness:
- the “–name” flag allows us to start/stop our container easily using the name rather than an ID;
- the “-p” flag exposes the port so that other computers on the network can access the database; and
- the “-v” flag maps the local directory to the /data/db directory in the container (where Mongo stores its data).
When using Docker Compose and building more advanced systems it is recommended to use named volumes. The problem with named volumes is that all the data is stored in “/var/lib/docker/volumes/” and I have some big databases that mean I’m constantly running out of space. One solution is to maybe mount a logical volume to “/var/lib/docker/volumes/” before creating the volumes that is managed separately from the “/” logical volume.