Is there an easy way to clear a mongodb database running in docker?
Or possibly delete it all and create it again?
There are a number of ways:
Regarding the last option, you shouldn't be in this scenario because your data should live on the host system and your container should be disposable.
I had a case with slightly different requirements:
docker-composeMONGO_USER & MONGO_PASSWORD already set)So, for anyone who might need a handy one-liner to authenticate and drop a mongo db with docker, here it is 👇:
docker-compose exec mongo_container_name /bin/bash -c 'mongo database_name -u $MONGO_USER -p $MONGO_PASSWORD --authenticationDatabase admin --eval "db.dropDatabase();"' And if you add a Makefile to the "mix", it's exactly the same but it needs $$ to the variables:
db-reset: docker-compose exec mongo_container_name /bin/bash -c 'mongo database_name -u $$MONGO_USER -p $$MONGO_PASSWORD --authenticationDatabase admin --eval "db.dropDatabase();"'
docker container ls --allthendocker container rm <id>, then build again