dockerdocker-container

Why my MongoDB data still persists even when I use it without docker volume?


Here's an explanation of what I am doing in steps:

  1. Start a mongo container using the command

docker run -p 27017:27017 -d mongo

  1. After that I start a connection in MongoDB compass using the connection string

mongodb://localhost:27017

  1. After that I create a database named "Test" and a collection named "dummy" and add two dummy users manually using Mongodb compass.

  2. After that I killed the container using

docker kill <container_id>

  1. I then disconnect from the Mongodb compass and close it.

  2. Then I create a new container using the same command

docker run -p 27017:27017 -d mongo

  1. Then I opened the Mongodb compass and found out the dummy database I created on the previous container still persists in

mongodb://localhost:27017

But according to the Docker concept since I am not using volumes the data is not supposed to persist. I even tried removing all the containers manually but it still persists. I tried connecting to mongodb://localhost:27017 without any container running and it still persists.

What am I missing here?


Solution

  • First of all I would verify that there are actually no volumes being used. You can do this by running docker volume ls, or if you're using Docker Desktop, then check the Volumes tab. Docker may sometimes use anonymous volumes, so it's worth checking, see if there's any and they stand out.

    Also make sure you don't have any local installations of MongoDB running on localhost:27017. Try running ps aux | grep mongo or similar commands, depending on your OS.

    Lastly I would try deleting the stopped containers, images, networks, etc. Just to get a clean picture.