Install Mongodb In Docker

/ Comments off

1. Version selection

Address of the image warehouse accessing mongodb:https://hub.docker.com/_/mongo?tab=tags&page=1
Select the latest version here for installation. If you want to install other available versions, you can use the command “docker search Mongo” to view them

2. Pull the latest version image

Deploying an Instance of MongoDB as a Container. With the image available to us, we need to deploy it. In its simplest form, and what is outlined in the Docker Hub documentation, we can execute the following command: docker run -name mongodb mongo:4.0.4. This command works, but there are potentially a few problems. 🐳 Deploy a Docker Container: FREE project forever 😘)In less than 60 seconds this video, I’. Docker quickly deploys standalone MongoDB containers. If you are looking for fast deployments of both replica sets and shards, I suggest looking at the mlaunch tool. Peter mentioned, in his article, how MySQL has two different “official” repositories, and with MongoDB, it’s the same: MongoDB has one repository maintained by MongoDB. This means that the container that you are trying to pull is not available for the architecture, in this case it would be linux/arm64. Maybe there is an image available now. I would search for 'docker mongodb arm64' to see. I see support for mongodb under arm64v8 architecture, which would be in Raspberry Pi 4.

Here, execute the command “sudo docker pull Mongo: latest” to pull the latest version of mongodb image file
After the image pulling is completed, view the image pulled from the drop-down through the command “sudo docker images”. You can see that the image has been successfully pulled to the local
Install Mongodb In Docker

3. Start operation

Run the startup command “docker run – P 27017:27017 – V / data / Mongo: / data / DB — name mongodb – D Mongo”
In the above command, several command parameters are explained in detail as follows:
-P map the 27017 port of the container service to the 27017 port of the host. External users can directly access Mongo’s services through the host IP: 27017
-V is to set the mounting directory of the container. Here, mount the local “/ data / Mongo” directory into / data / DB in the container as the storage directory of mongodb
-D set the container to run as a daemon
Use the command “docker PS” to check the startup and operation of the container
You can see that the Mongo container has run successfully

4. Use the client tool studio 3T to connect Mongo

Studio 3T download address:https://studio3t.com/download/

5. Create Mongo user

You can see an empty Mongo database. One imperfection is that our Mongo does not have any account password. In streaking, we execute the following statement to create an administrator user,

6. Specify auth to re run the service

After creating the user, we will restart the Mongo service and specify auth
Delete the already running Mongo container: docker RM – F mongodb
Specify the validation startup Mongo container: docker run – P 27017:27017 – V / data / Mongo: / data / DB — name mongodb – D Mongo — auth
That is, add — auth after the previous startup command: you need a password to access the container service

Install Mongodb In Docker Windows 10

At this time, you can see the prompt when you forcibly access, and there is no authorization
Here we specify the account password at the connection place
Query again. The user information added before has been correctly queried

7. Create a business database and corresponding read-write accounts

One of the most accessible NoSQL databases, MongoDB®, has many availabledeployment options. In this post, I use Docker® to deploy MongoDB as acontainer and use the shell client to interact with that container.

Introduction

After you install Docker on your computer or server, perform the following stepsto deploy MongoDB as a Docker container:

  1. Download the latest or a specific MongoDB Docker image.
  2. Deploy an instance of MongoDB as a container.
  3. Interact with the MongoDB Docker container with basic shell operations.

Download the MongoDB Docker image

If you have Docker installed on your computer or server, you can get the MongoDBimage from the Docker Hub container registry. Be sure to download the specificimage version that you expect to use, such as mongo:4.0.4. You don’t have tospecify the image version, but if you omit it, you get the most current version,which could result in an inconsistent experience because Mongo can update thelatest version at any time.

Executing the docker images command gives a list of images available on theserver. In our case, it shows the MongoDB image with the tag 4.0.4 available foruse.

Use the following commands to download the MongoDB image and list your availableimages:

Deploy an instance of MongoDB as a container

Start the container in detached, -d, mode. Because containers use ephemeralstorage, you should mount a volume, -v, to retain the data by providing thelocal path, /data/db. This path is the container’s internal directory. Thisaction preserves the data on the host’s /data/db path even when you stop orremove the container. In the command, you provide a name, --name, for yourcontainer followed by the Docker image and a tag. In this case, I usedmongo:4.0.4 for the tag. The command returns a string with thecontainer’s ID. The docker ps command shows a list of running containers.

Interact with the MongoDB Docker container with the shell

To take advantage of a functional MongoDB deployment, you can interact withthe database by using the shell client.

How To Install Mongodb In Docker

Because you are running the container in detached mode, you should connect byusing the interactive terminal. Use the following command to connect to yourdeployment, named mymongo, and start the bash shell:

To launch the MongoDB shell client, run the following command:

From the MongoDB shell client, you can access all the functions outlined in theMongoDB documentation. For example, you can see what databases exist in yourinstance with the following command:

Install Mongodb In Docker Windows

Conclusion

In this post, I created a MongoDB container by using Docker with an attachedstorage volume from a host system to a container. I also showed you how to startinteracting with the MongoDB Docker container through the shell. The post offersa basic demonstration of running a MongoDB instance in a Docker container, butyou can use the same MongoDB image to build any number of containers. Use thosecontainers to create replica sets and do much more to create a robustcontainerized application.

I hope this post helped you to get a general understanding of how to get aMongoDB instance up and running and to start working with it in minutes.

Install Mongodb In Docker Command

Use the Feedback tab to make any comments or ask questions. You can alsochat now to start the conversation.