How to mount folder that contains db data ( file system ) outside docker container, so that if somehow docker container is stopped we can persist data ( get back the data ) on restarting the docker container ?

Steps to store data in volume

  1. Create volume ( This will exists irrespective of the container , this way state gets persisted outside container )
docker volume create <VOLUME_NAME>

# docker volume create mongo_db_data
  1. Store the contents inside /data/db of mongo container to volume here mongo_db_data
docker run -p 27017:27017 -v mongo_db_data:/data/db mongo
docker volume ls # List all existing volume

Q Why we should never mount multiple mongo db container to same volume ?

It will create concurrency issues. Overwriting file system

Networks

image.png

const mongoUrl: string = `mongodb://localhost:27017/myDatabase`;

// If we start node js process on a docker container by running

docker run -p 3000:3000 node-app 

// "localhost" would mean localhost of node-app container, and no process is running on localhost:27017 of node-app container

How to connect to mongo db running on a different container, from node app container?