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.

  • how does docker handle file creation?
  • how to make docker logs in java
  • What is exactly happening when you're Docker Commiting a container?
  • Local WordPress env with Docker Compose - cURL error 7: Failed to connect to localhost port 8080: Connection refused
  • Is it possible to prevent RedHat/CentOS Docker host root access from within a container?
  • How to find css/js files on a server for specified application that uses docker
  • 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?

  • `docker-machine scp` from local directory to machine
  • Gradle build fails while executing './gradlew build installDist --info' inside docker container
  • docker container exits immediately even with Console.ReadLine() in a .net core console application
  • Running VirtualBox in Ubuntu in Mac OSX, won't start up
  • Rancher Container Utilization stats stuck on Connecting
  • Connecting to a USB Android Device in a Docker Container via adb
  • 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.