We just verified that after Docker compose down up commands all data inside of the mongo container was
lost.
And the reason for that is that Docker Compose creates a brand new container each time when you enter
Docker, compose down and up commands.
And now let's add volumes mapping for mongo container in order to be able to persist MongoDB data and
any container that is started using Mongo image will utilize the same volume and all MongoDB data therefore
will be persistent and this volume will be created inside of the docker, not on your local computer.
Why?
Because actually we could perform volume mapping similarly as we did, for example, for API service.
Here we mapped a folder that is located on your computer here to the app folder inside of the container.
And actually container is now able not just to read data from the folder when it is running, but also
it is able to write data to the folder if necessary.
It is bidirectional communication.
And actually we are able to now create, for example, here a new folder and name it, for example MongoDB
and afterwards map this volume to the mongo container here using similar approach as we did here.
But in such case, Mongo will actually write actual data to the MongoDB folder on your computer and
it's not really convenient because this is images gallery project folder here you should store only
project related files like JavaScript files, python files and so on.
You should not store here some database information.
That's why such approach is not convenient and in Docker it is possible to create volume inside of the
Docker itself because what Docker is in a nutshell, it is just virtual machine on your macOS or Windows
computer.
And inside of that virtual machine it is possible to create volumes and map those volumes to the containers.
And in such case, those volumes inside of the Docker will be persistent and they will be reusable by
different containers.
Notice that if you are running Linux like system and install Docker there, you actually run Docker
Community Edition, not Docker Desktop and their volumes will be created directly on your computer.
But for Windows and macOS users, Docker is just virtual machine and you could create volumes inside
of that virtual machine and reuse them inside of the containers.
And that's what we are about to do right now.
Let's create new volume and for that you need to add instructions here at the root of the Yaml file
and instruction is volumes like that.
And here let's add new volume named for example MongoDB underscore data and make sure to add here colon
afterwards with such syntax in place.
Docker compose will create new volume called MongoDB underscore data inside of the Docker.
And now we are able to use such volume and map it to containers and let's map it to mongo container
here.
And syntax is similar as we saw here volumes.
Like that.
And on the next line will be minus sign.
And here are type simply name of the volume you want to map and it is MongoDB data.
And note this suggestion from vs code MongoDB underscore data.
And notice that there was no dot or forward slash here in front of the name of the volume, because
in such way Docker will understand that you want to map internal Docker volume called MongoDB data volume
that was created here.
If you add here dot and forward slash, it will mean for Docker that you want to map volume on your
local computer and it will try to find folder MongoDB underscore data in your project.
Similarly, as it finds a folder or front end folder for corresponding containers.
That's why you need to type here.
Simply MongoDB underscore data.
If you want to map volume from the Docker itself and here after colon, we need to specify destination
folder inside of the mongo container and all data inside of the mongo is stored in following path.
Data slash debe.
It is default database path inside of the mongo server and that's actually it.
Let's save changes and quickly summarize here.
We created custom volume inside of the docker and this volume is called MongoDB Underscore data.
You could give other name to this volume if you want to do so.
And afterwards we map such custom volume.
MongoDB underscore data to the data DB folder inside of the mongo container and in such case, any container
that will be started by Docker compose will actually be connected to MongoDB, underscore data volume
and any data that is created by one container inside of this volume will be reusable by other containers
that are connected to the same volume.
That's how we will persist data in the Mongo database.
And now let's of course verify that on practice and let's proceed with that after the small pause.