How to Install MongoDB using Docker

How to Install MongoDB using Docker

In Previous Chapter , I explained to install mongodb in linux , In Today’s chapter I will explain

how you can install MongodB using Docker, which is recommended and very easy to manage.

First you need to install Docker in your system either Linux, Windows or Mac.

Now go to and copy below command and run in your docker.

docker run -p 27017:27017 -d -v /var/www/html/docker/mongo:/etc/mongo mongo:latest

So If you will see I have docker run command which is to run any container into your docker system.

then I have mentioned -p and port number so you can able to access mongodb outside of docker container so if you running it in your local then you will be able to access like mongodb://localhost:27017 and can connect with any of your local application, if you will not use -p then you cannot use localhost and need to use container assigned IP only.

Now if you will I have mentioned -v , which is for volumn , means you are mounting container storage to your local storage, it helps you to loss your data. so suppose you shutdown your mongo container and later on you started again then if you will not define volumn then you will loss your data. so here /var/www/docker/mongo is your local path where mongo data will be saved.

and lastly I have mentioned application name which is mongo and version which is latest.

and also I have mentioned -d means it will keep running your mongo db and you will come out of container.

You can as below when you run docker ps

Now to access your mongo db you can use below command

docker exec -it 40a37ccd00ff /bin/sh

Here 40a37ccd00ff is your container Id.

and then you can run mongo to run your mongo db shell

Hope you find this tutorial useful, please come again.

Leave a Reply
Your email address will not be published. *