Docker - Setting up a MongoDB Container
Last Updated :
31 Oct, 2020
MongoDB is a NoSQL database that is used in many web applications nowadays to store the data in the form of objects. Where on the other side docker is also getting so popular to launch the server fast and with using less space to launch it. So docker has created the MongoDB image to launch its container. If you launch the MongoDB image in docker then it will listen on MongoDB port 27017. One can use this combination in this way if you are deploying any web application on the server launch on docker in the backend if you want to store any data then you can use MongoDB as a database. This complete process is faster than any other technology used to deploy the application.
In this article, we will see how to launch the MongoDB image in docker and how to connect two MongoDB containers in which one will act as a client and others will act as a server.
Setting up a MongoDB in Docker
Follow the below steps to set up a MongoDB Container in Docker:
Step 1: To launch any container in docker first you want the image of that particular container so the first step is to log in to your docker hub and search for MongoDB and click on the official image option.
Step 2: When you click on the link you will see the pull command copy that command.
Step 3: Go to your docker host OS and paste that command there it will pull the latest version of MongoDB image into your docker host.
Step 4: After downloading the image now it's time to launch the container. The command is
sudo docker run -it -d mongo
Description of command:
-it: This option is used to run the container in iterative mode.
-d: This option is used to run containers as a daemon process.
Now run the docker ps command to see the details of the docker container.
Notice the name of the container which is different for everyone here it is priceless_dijkstra and the port number which is 27017/tcp in this case.
Step 5: Now launch another container that will act as a client and connect to the MongoDB database.
sudo docker run -it -link=priceless_dijkstra:mongo mongo /bin/bash
Description of command:
In this command, we are linking the pre-existing container with the new mongo container which we are launching my mentioning the mongo in the command.
Now we are inside the new container.
Step 6: Use the env command to see the details of the new container.
env
Step 7: Now we are going to connect the MongoDB server container to the client container.
mongo IP:port_number
The IP and the port number you will get by using the env command and mongo command is used to connect to the mongo database. After running this command you will connect to the database then you can run any MongoDB command.
Similar Reads
Mounting a Volume Inside Docker Container When you are working on a micro-service architecture using Docker containers, you create multiple Docker containers to create and test different components of your application. Now, some of those components might require sharing files and directories. If you copy the same files in all the containers
10 min read
Docker - Container Linking Docker is a set of platforms as a service (PaaS) products that use the Operating system level visualization to deliver software in packages called containers.There are times during the development of our application when we need two containers to be able to communicate with each other. It might be p
4 min read
How to Seed a MongoDB Database Using Docker Compose Seeding a MongoDB database is a common task in many development and testing scenarios. It involves populating the database with initial data to ensure consistent and predictable behavior during the application development and testing phases. Docker Compose is a powerful tool that simplifies the proc
4 min read
How to use MongoDB Connection String MongoDB connection strings are essential for establishing connections between applications and MongoDB databases. These strings contain crucial information such as server addresses, authentication credentials and optional parameters, enabling seamless communication. Understanding the structure and c
6 min read
How to Connect to MongoDB Atlas Using Shell? MongoDB is a highly scalable NoSQL database, renowned for its ability to manage vast amounts of complex and unstructured data. Unlike traditional databases that use a tabular format, MongoDB employs a document-oriented approach, storing data in a flexible, JSON-like format. This makes MongoDB highly
5 min read
Docker - Docker Container for Node.js Node.js is an open-source, asynchronous event-driven JavaScript runtime that is used to run JavaScript applications. It is widely used for traditional websites and as API servers. At the same time, a Docker container is an isolated, deployable unit that packages an application along with its depende
12 min read