Docker – MongoDB

By | 10/11/2021

In these previous posts:
MongoDB – How to install it on macOS
MongoDB – How to install on Windows
we have seen how to install Mongo DB.
In reality, with DOCKER, we can use MongoDB without install it on our system.

IMPORTANT: For this post, I have used a Windows system but it is the same with Mac or Linux.

We open a terminal instance and, using the command docker images, we can check that we don’t have any images in our system:

Now, with the command docker pull mongo, we will get the Mongo image:

Running the command docker image inspect mongo (where mongo is the image’s name), we can check the properties of the image:

Now, we will run this image in a container that we will call “dockermongo” and where we will define an user Admin, using the command:

docker run -d --name dockermongo -e MONGO_INITDB_ROOT_USERNAME=admindb -e MONGO_INITDB_ROOT_PASSWORD=pass123 mongo

Then, using the command docker exec -it dockermongo bash, we will execute the bash command inside the container, in order to manage our Mongo DB instance:

Finally, running the MongoDB command show dbs, we will see the list of databases :

In this case, we can see that the command didn’t work and the reason is because we have created this container by defining User and Password and so, first of all, we have to login:
use admin
db.auth(“admindb”, passwordPrompt())

Now, if we run the command show dbs again, this will be the result:

Leave a Reply

Your email address will not be published. Required fields are marked *