How to Run a MongoDB Server with Docker?

MongoDB is a well-known NoSQL document-oriented database management system. It stores data as collections of documents. MongoDB server is the software that permits users to run the MongoDB database. It is responsible for managing data storage, querying, and indexing. Users can run MongoDB server with Docker to easily deploy and manage MongoDB instances in a containerized environment.

This article will explain the procedure of running a MongoDB server with Docker.

How to Run a MongoDB Server Using Docker?

To run a MongoDB server with Docker, check out the below-provided steps:

Step 1: Pull MongoDB Image From Docker Hub

First, pull the MongoDB image from Docker Hub using the below-provided command:

docker pull mongo

The latest version of the MongoDB image has been downloaded successfully.

Step 2: Build and Start MongoDB Container

Then, use the below-provided command to start the MongoDB container and execute the MongoDB server:

docker run -d --name mongoCont -p 27017:27017 mongo


  • -d” is utilized to execute the container in the background.
  • –name” assigns a name to the container i.e., “mongoCont”.
  • -p” allocates the port for the container i.e., “27017:27017”.
  • mongo” is the official Docker image to use for the container:

This command starts the MongoDB server.

Step 3: View Running Container

Next, view the running MongoDB container with the help of provided command:

docker ps

The output indicates that the MongoDB container is running successfully.

Step 4: Access MongoDB Container

After that, use the “docker exec -it” command along with the container name to open the Bash shell inside the running MongoDB container:

docker exec -it mongoCont bash

The MongoDB container has been accessed and now users can execute commands inside the running container.

Step 5: Check MongoDB Server

To verify whether the MongoDB server is running or not, type out the below-listed command:

mongod --version

It can be observed that the MongoDB server is running successfully with version “v6.0.5”.

Step 6: Launch MongoDB

To connect to the MongoDB server, open the Mongo shell using the following command:


The above output shows that the Mongo shell has been opened successfully.

Step 7: View Existing Databases

Finally, write out the “show dbs” command to view the existing databases:

show dbs

In the above output, all the existing databases can be seen.


To run a MongoDB server with Docker, first, pull the latest version of the MongoDB image from Docker Hub. Then, start the MongoDB server using the “docker run -d –name <container-name> -p 27017:27017 mongo” command. Next, access the MongoDB container and check the MongoDB server. After that, connect to the MongoDB server and execute MongoDB commands. This article has explained the procedure to run a MongoDB server with Docker.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.