MongoDB

How to Run MongoDB as a Docker Container?

MongoDB is one of the most used and open-source No-SQL databases. It is used to manage, store, and search raw data or semi-structural data. Instead of tables and complex schemas, it stores data in documented form in JSON format. MongoDB can be accessible on various platforms and installable on Windows, and Linux OS. However, users can also use the MongoDB database in a portable Docker container.

This blog will demonstrate how to execute MongoDB as a Docker Container.

How to Run MongoDB as a Docker Container?

Docker is a well-developed open-source platform that is used to create and run portable, light weighted, and isolated containers. Docker containers can also run databases like PostgreSQL, MySQL, and MongoDB due to their high scalability and isolation among applications and containers.

To run MongoDB as a container, go through the listed steps.

Step 1: Search Mongo Image on DockerHub

First, open the Docker Hub official Docker registry and search for the “mongodb” Docker image and open the official “mongo” image:

Step 2: Pull the MongoDB Image

Next, pull or download the image on your system. For this purpose, first, copy the below-highlighted command:

Then, open the Windows terminal and execute the copied command:

docker pull mongo

Step 3: Run the Container

Next, run the “mongo” Docker image to create and start the Docker container and run MongoDB as a container:

docker run -d -p 27017:27017 --name=mongoDB-cont mongo:latest

For the verification, visit the “localhost:27017” URL and check if mongoDB is running in container or not:

Step 4: Start Using MongoDB in Container Shell

Next, to access the MongoDB shell, utilize the “docker exec” command along with the container name and “mongosh”. The “mongosh” is used to open the command line shell:

docker exec -it mongoDB-cont mongosh

Step 5: Create New Database

Next, create the new database using the “use <Database-name>” command. This command will automatically create and switch the database:

use Linuxhint

Step 6: Create New Collection

After that, create the new collection that is also referred to as a document to store and manage data in JSON format:

db.createCollection("author")

Step 7: Insert Data in MongoDB

Insert the data in the “author” MongoDB collection as shown below:

db.author.insertMany([ {name: "Rafia", designation: "Technical-Author"}, {name: "Maria", designation:"Technical-Author" } ])

Step 8: Access Data Using “find()”

To search or find the data from the collection, utilize the “find()” function along with the “collection” name of the current database:

db.author.find().pretty()

From the output, you can see that we have successfully accessed the data from the “author” collection:

In order to exit the MongoDB database and shell, use the “exit” command:

exit

Bonus Tip: How to Run MongoDB as a Docker Container Using Docker Compose?

To run MongoDB as a Docker container, you can configure the MongoDB service in a compose file and execute the MongoDB as a container using Docker compose. For the demonstration, follow the listed steps.

Step 1: Create “docker-compose.yml” File

Configure the MongoDB service in the compose file using the below provided snipped. The following settings are configured in compose file:

  • The “mongo” service is used to configure the setting to run the MongoDB container.
  • image” defines the Docker image used to run the MongoDB as a container.
  • The “container_name” key is used to assign the name to the container.
  • “environment” is utilized to set the environment variable for the MongoDB container such as username and password.
  • port” defines the MongoDB localhost exposing port:
version: '3.1'
services:
mongo:
image: mongo
container_name: mongoDB
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: root123
ports:
- 27017:27017

Step 2: Start Compose Service

Next, fire up the container to run the MongoDB database using the “docker-compose up” command:

docker-compose up -d

Step 3: Start MongoDB  Command line Shell

Lastly, start the MongoDB container shell to use the MongoDB database:

docker exec -it mongoDB mongosh

That’s all about executing MongoDB as a container in Docker.

Conclusion

To run MongoDB as a Docker container, first, get the “mongo” image from the official Docker Hub registry using the “docker pull mongo” command. After that, run MongoDB as a container by executing the “mongo” image through the “docker run” command. In the next step, utilize the “docker exec -it <container-name> mongosh” command to start using the MongoDB command line shell within a container. This post has demonstrated how to run MongoDB as a container.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.