3

I have created the following docker-compose.yml file

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

Then started it with

sudo docker-compose up

Then connected to mongo and created few documents. Then I have restarted my compose. Surprisingly, the data persists. As far I remeber, Docker was forgetting data if no volumes configured. Is this changed?

Where is it keeping my data in this situation?

Dims
  • 47,675
  • 117
  • 331
  • 600
  • 3
    Your data remain inside the container. `docker-compose stop/start` doesn't remove the containers created, that's why the data are still there. If you `docker-compose down` (stop and remove) and then `docker-compose up` (create and run) the data will be lost. – tgogos Nov 23 '21 at 13:05
  • If you had used the docker compose volumes option the database data can be persisted outside the docker container and on the docker host instead. see https://stackoverflow.com/questions/68517126/first-time-with-mongodb-docker-set-up-from-docker-compose/68552274#68552274 – barrypicker Nov 23 '21 at 19:16

2 Answers2

1

By default, MongoDB’s default data directory path is \data\db and be keeped inside your container, so as long as the container is not completely removed, you can resume your container and data still stay there. In this case, if you restart MongoDB by docker-compose stop/start, it'll not remove your containers, just stop them and start again, so your data may persist. Unless you was using docker-compose down/up, which will completely remove and recreate new containers, then your data will be wiped.

Viettel Solutions
  • 1,519
  • 11
  • 22
0

take a look at the directory /var/lib/docker

https://betterprogramming.pub/persistent-databases-using-dockers-volumes-and-mongodb-9ac284c25b39

You can list you docker container with

docker ps

and then get the mount volume

docker inspect -f '{{ .Mounts }}' containerid

https://stackoverflow.com/a/30133768/5857581