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
DevOps Tutorial DevOps is a combination of two words: "Development" and "Operations." Itâs a modern approach where software developers and software operations teams work together throughout the entire software life cycle, from planning and coding to testing, deploying, and monitoring.The main idea of DevOps is to i
9 min read
Amazon Web Services (AWS) Tutorial Amazon Web Service (AWS) is the worldâs leading cloud computing platform by Amazon. It offers on-demand computing services, such as virtual servers and storage, that can be used to build and run applications and websites. AWS is known for its security, reliability, and flexibility, which makes it a
13 min read
Docker Tutorial Docker is a tool that simplifies the process of developing, packaging, and deploying applications. By using containers, Docker allows you to create lightweight, self-contained environments that run consistently on any system, minimising the time between writing code and deploying it into production.
7 min read
What is Docker? Have you ever wondered about the reason for creating Docker Containers in the market? Before Docker, there was a big issue faced by most developers whenever they created any code that code was working on that developer computer, but when they try to run that particular code on the server, that code
12 min read
Complete DevOps Roadmap - Beginner to Advanced DevOps is considered a set of practices that combines the abilities of Software Development i.e Dev and IT Operations i.e Ops together, which results in delivering top-notch quality software fastly and more efficiently. Its focus is to encourage communication, collaboration, and integration between
8 min read
What is CI/CD? CI/CD is the practice of automating the integration of code changes from multiple developers into a single codebase. It is a software development practice where the developers commit their work frequently to the central code repository (Github or Stash). Then there are automated tools that build the
10 min read
Kubernetes Tutorial Kubernetes is an open-source container management platform that automates the deployment, management, and scaling of container-based applications in different kinds of environments like physical, virtual, and cloud-native computing foundations. In this Kubernetes Tutorial, you are going to learn all
8 min read
DevOps Interview Questions and Answers 2025 Preparing for a DevOps interview? Whether you are a fresher or an experienced professional with 3, 5, or 8+ years of expertise, this guide covers essential DevOps interview questions to help you crack your next job interview.DevOps is a high-demand field in 2025, and companies look for candidates wi
15+ min read
Microsoft Azure Tutorial Microsoft Azure is a cloud computing service that offers a variety of services such as computing, storage, networking, and databases. It helps businesses and developers in building, deploying, and managing applications via Microsoft-Controlled data centers. This tutorial will guide you from Microsof
13 min read
What is DevOps ? DevOps is a modern way of working in software development in which the development team (who writes the code and builds the software) and the operations team (which sets up, runs, and manages the software) work together as a single team.Before DevOps, the development and operations teams worked sepa
10 min read