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.

  • Docker for Mac VM IP
  • Amazon ECS error: cannot create a task definition with no containers
  • Creating multiple Docker container
  • Docker copy container volume files to host on first run
  • Translate bash command to dockers CMD
  • Running Python Script in Background Infinitely
  • 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?

  • Wercker crashes on MacOS with PANIC too many open files
  • how to clear cache memory inside docker container
  • Docker name gets longer each time I run docker-compose
  • Jenkins: running build does not provision docker slave
  • how to handle redirection url in port forwarding - docker container
  • What's the difference when installing docker with 2 of these following command?
  • 3 Solutions collect form web for “Keep data in the external folder for mongodb docker container”

    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.

    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
    

    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.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.