I would like to persist the mongoDB
data outside of the container and on a specified volume. I am using docker-compose and the yml file looks like
web: build: . command: python -u app.py ports: - "5000:5000" volumes: - .:/todo links: - db db: image: mongo:3.0.2
If you need to access the MongoDB server from another application running locally, you will need to expose a port using the -p argument. Using this method, you will be able to connect to your MongoDB instance on mongodb://localhost:27017 . You can try it with Compass, MongoDB's GUI to visualize and analyze your data.
For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.
You can connect to MongoDB on localhost:27017 . Then use the following command to open the MongoDB shell. I have used mymongo as an arbitrary container name, though you can replace mymongo with test-mongo or any other container name of your choosing. The show dbs command will display all your existing databases.
As documented on the docker hub page for that image (https://hub.docker.com/_/mongo/) you can use
volumes: - './data:/data/db'
Which will use the host path ./data
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With