English 中文(简体)
Docker - Setting MongoDB
  • 时间:2024-12-22

Docker - Setting MongoDB


Previous Page Next Page  

MongoDB is a famous document-oriented database that is used by many modern-day web apppcations. Since MongoDB is a popular database for development, Docker has also ensured it has support for MongoDB.

We will now see the various steps for getting the Docker container for MongoDB up and running.

Step 1 − The first step is to pull the image from Docker Hub. When you log into Docker Hub, you will be able to search and see the image for Mongo as shown below. Just type in Mongo in the search box and cpck on the Mongo (official) pnk which comes up in the search results.

Mongo DB

Step 2 − You will see that the Docker pull command for Mongo in the details of the repository in Docker Hub.

Docker Pull Command For MONGO

Step 3 − On the Docker Host, use the Docker pull command as shown above to download the latest Mongo image from Docker Hub.

latest Mongo Image

Pull Command MongoDB

Step 4 − Now that we have the image for Mongo, let’s first run a MongoDB container which will be our instance for MongoDB. For this, we will issue the following command −

sudo docker run -it -d mongo

The following points can be noted about the above command −

    The –it option is used to run the container in interactive mode.

    The –d option is used to run the container as a daemon process.

    And finally we are creating a container from the Mongo image.

You can then issue the docker ps command to see the running containers −

MongoDB Container

Take a note of the following points −

    The name of the container is tender_poitras. This name will be different since the name of the containers keep on changing when you spin up a container. But just make a note of the container which you have launched.

    Next, also notice the port number it is running on. It is pstening on the TCP port of 27017.

Step 5 − Now let’s spin up another container which will act as our cpent which will be used to connect to the MongoDB database. Let’s issue the following command for this −

sudo docker run –it –pnk=tender_poitras:mongo mongo /bin/bash 

The following points can be noted about the above command −

    The –it option is used to run the container in interactive mode.

    We are now pnking our new container to the already launched MongoDB server container. Here, you need to mention the name of the already launched container.

    We are then specifying that we want to launch the Mongo container as our cpent and then run the bin/bash shell in our new container.

Bin Bash

You will now be in the new container.

Step 6 − Run the env command in the new container to see the details of how to connect to the MongoDB server container.

ENV Container New Command

Step 6 − Now it’s time to connect to the MongoDB server from the cpent container. We can do this via the following command −

mongo 172.17.0.2:27017 

The following points need to be noted about the above command

    The mongo command is the cpent mongo command that is used to connect to a MongoDB database.

    The IP and port number is what you get when you use the env command.

Once you run the command, you will then be connected to the MongoDB database.

MongoDB database

You can then run any MongoDB command in the command prompt. In our example, we are running the following command −

use demo

This command is a MongoDB command which is used to switch to a database name demo. If the database is not available, it will be created.

Database name DEMO

Now you have successfully created a cpent and server MongoDB container.

Advertisements