ceth
ceth

Reputation: 45325

Keep data in the external folder for mongodb docker container

I am using official docker container. Here is a Dockerfile. I am running container using this command:

docker run --name mongo-db -d -p 27017:27017 -v  /mnt/lacie/databases/mongo/data:/data mongo

Next I connect to mongodb, create a db, a collection and insert data on it.

I can stop the container docker stop, run it again docker start and all the data changes will be present in the database.

If I remove container docker rm and create new one - the data dissapear. Why? And how can I fix it?

Upvotes: 3

Views: 4088

Answers (3)

zicjin
zicjin

Reputation: 351

I have the same problem. the cause of the problem is not to use rm -f to remove the mongo container, we must to first command "stop" and then "rm" the mongo container.

Upvotes: 0

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54810

Try debugging it like this:

docker run --name mongo-db -d -p 27017:27017 -v /data mongo
docker stop mongo-db
docker run --name mongo-db2 -d -p 27017:27017 --volumes-from mongo-db mongo

If that works, then there's in an issue in the way mongo db is storing the data (I see in the Dockerfile for example that it specified /data/db as a VOLUME so it might expect the rest of the /data/ folder to be empty on initialization). That would probably be a bug but I'm just guessing without further info.

You can test my theory using:

docker run --name mongo-db -d -p 27017:27017 mongo
docker stop mongo-db
docker run --name mongo-db2 -d -p 27017:27017 --volumes-from mongo-db mongo

Upvotes: 0

Peter Lyons
Peter Lyons

Reputation: 146094

Based on the current Dockerfile you need to mount under /data/db not just /data. Because of this mismatch your data is probably going into the container's private storage instead of into your mounted volume.

Upvotes: 5

Related Questions