Skip to content

Run MongoDB in Docker Container AQSA Spark By {Examples}

  • by

Using Docker to run MongoDB can simplify the deployment process and make it easier to manage the database infrastructure. We shall determine how to set up MongoDB in a Docker in this article. The steps to run MongoDB inside a Docker container are listed below.

Following are step-by-step instructions on how to install and run MongoDB in a docker container.

1. Method to set up MongoDB in a Docker

The following method can be followed to set up MongoDB in a Docker container.

1.1 Configure the Installation of Docker

Configuring whether Docker is already installed on our system or not is the first step. If it’s not already installed, then it is required to be installed to get the MongoDB instance.

#Checking docker version
docker version

The command shows the version information for the Docker client and server components installed on the system. Also, we have displayed the output of the server version along with the build information.

To install docker on your system, you can follow this link.

1.2 Pull the Docker Image of MongoDB

The next action is to get the MongoDB image from the Docker Hub repository to set up MongoDB in Docker. The following command of Docker will get the image of MongoDB.

# Getting mongoDB image
docker pull mongo:latest

When we run this command, Docker searches for the mongo image on the Docker Hub registry and downloads it to the local machine along with all the requirements needed to execute the containerized MongoDB instance. Furthermore, we can see from the output of running the above command gets all the libraries of MongoDB into the Docker container and the status verifies that the newer image for MongoDB is downloaded successfully.

1.3 Run the MongoDB Image

Here, we use the Docker run command to launch a containerized MongoDB instance after the MongoDB image has been downloaded. The command is given below to run the image of MongoDB as a container.

# Running MongoDB image as a container
docker run -d –name mongodb -p 27017:27017 mongo

Above, the -d flag starts the container in the background, which means that the container will run in detached mode. Then, the  –name option is used to give a name to the new container, in this case, mongodb. After that, the -p flag maps the given port container to the set port of the host.  This enables us to access the MongoDB instance running inside the container using the host system’s IP address and port number. Hence, it will give the following output when executed successfully.

1.4 Verify the Container is Running

Now, we can validate whether the container is running or not by using the docker with the ps command. To view a complete list of the Docker containers that are active on the system right now, execute the following command.

# Checking docker containers list
docker ps

There, we can see the MongoDB inside the Docker container successfully running below.

1.5 Connect the MongoDB to the Docker Container

Finally, we can connect with the MongoDB shell from the Docker containers by using the following command.  

# Connecting MongoDB to docker container
docker exec –it mongodb mongosh

When we run this command, Docker starts a new process inside the running mongodb container, launches the MongoDB shell (mongosh), and connects to the MongoDB instance running inside the container to set up MongoDB in a Docker. Here, the exec option specifies running a command in a Docker container that is already executing. The –it option runs the command in interactive mode and mongodb is the name of the running container. The mongosh is the shell we want to run inside the container. Thus, we can see the mongosh shell is launched inside the container and is ready to accept commands.

1.6 Validate the MongoDB Command is Running

At last, we run the MongoDB hello command to verify that commands are running inside the containers. The command structure is as follows. This is an important step to set up MongoDB in Docker.

# Validation
db.runCommand({hello:1})

The results validate the command by outlining the MongoDB deployment below.

7. Conclusion

In conclusion, we have explained each step in detail to install and run the MongoDB instance in Docker. These are the fundamental procedures to set up Docker to run MongoDB. By using all these steps, we can easily run MongoDB in Docker.

Additionally, if you want to install MongoDB Enterprise with Docker, you can visit the following link.

 Using Docker to run MongoDB can simplify the deployment process and make it easier to manage the database infrastructure. We shall determine how to set up MongoDB in a Docker in this article. The steps to run MongoDB inside a Docker container are listed below. Following are step-by-step instructions on how to install and run  Read More MongoDB 

Leave a Reply

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