Updated on: Sat Sep 09 2023
There are cases happening quite often when you wanna check some functionality against data array provided from somewhere via mongodump utility. You may want to run some local server or just run some handcrafted queries. If you do not have mongodb installed locally then running it in docker sounds like a best way for me. So let's get our hands dirty a bit. Not too much luckily.
So first let's pull suitable mongodb docker image.
copied bash
docker pull mongo:4.2-bionic
Next let's run the container on some available port. In my case I did it on 8091 but we can use anything we want
copied bash
docker run --name test-mongo -d -rm -p 8091:27017 mongo:4.2-bionic
So now we have our mongo running and waiting for data being loaded there. To restore the dump we'll use mongorestore utility. There are several ways to get it installed on our system but I like have it as part of mongodb-database-tools. Go ahead to official docs and install the tools.
Other option to run mongo instance is to leverage docker-compose. For this create file docker-compose.mongo-dev.yml with content:
copied yaml
services:
db:
image: mongo:4.2-bionic
volumes:
- tmp_mongo_db-volume:/data/db
ports:
- 8091:27017
volumes:
tmp_mongo_db-volume:
To spin up container run followin command:
copied bash
docker-compose -f docker-compose.mongo-dev.yml up -d
And now you can connect to your running mongo instance via whatever mongo client you use. I prefer studio3t.
After that we can run mongorestore command pointing to our local running docker container with mongodb server.
copied bash
mongorestore --host localhost --port 8091 --db some_collection ~/path/to/dump/some_collection