Open In App
Related Articles

How to Run MongoDB as a Docker Container?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

MongoDB is an open-source document-oriented database designed to store a large scale of data and allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval of data in MongoDB are not in the form of tables. 

What Is a Docker Container?

Containerization is OS-based virtualization that creates multiple virtual units in the userspace, known as Containers. Containers share the same host kernel but are isolated from each other through private namespaces and resource control mechanisms at the OS level. Container-based Virtualization provides a different level of abstraction in terms of virtualization and isolation when compared with hypervisors. Hypervisors use a lot of hardware which results in overhead in terms of virtualizing hardware and virtual device drivers. A full operating system (e.g -Linux, Windows) runs on top of this virtualized hardware in each virtual machine instance. To Know more about Docker Containers refer to the Containerization using Docker.

To deploy your application in the form of containers you need to build a docker image by which you can run as a container to build an image you need to write the Dockerfile.

What is MongoDB?

MongoDB is an open-source document-oriented database that is designed to store a large scale of data and also allows you to work with that data very efficiently. It is categorized under the NoSQL (Not only SQL) database because the storage and retrieval of data in the MongoDB are not in the form of tables. The MongoDB database is developed and managed by MongoDB.Inc under SSPL(Server Side Public License) and initially released in February 2009. It also provides official driver support for all the popular languages like C, C++, C#, .Net, Go, Java, Node.js, Perl, PHP, Python, Motor, Ruby, Scala, Swift, and Mongoid. So, that you can create an application using any of these languages. Nowadays there are so many companies that used MongoDB like Facebook, Nokia, eBay, Adobe, Google, etc. to store their large amount of data. To Know more about MongoDB refer to the What is MongoDB &#x2013 Working and Features.

Can MongoDB Run in a Docker Container?

Yes, you can run MongoDB as a Docker container. In DockerHub there are two types of MongoDB images community edition and enterprise edition you can any of them according to the organization’s need to run as a Docker Container or you can build your own Docker Image by writing the Dockerfile according to your own requirements. To know about Dockerfile syntax refer to the What is Dockerfile Syntax?

How to Use MongoDB With Docker?

Steps For Running MongoDB As a Docker Container

Step 1:  Pull the MongoDB container.

docker pull mongo:latest

Pulling-MongoDB-container

Pulling the container

Step 2: Run the container.

docker run -d -p 27017:27017 --name=mongo-example mongo:latest


Running-the-container

Running the MongoDB container

Where the -d flag runs the container in detach (background) mode, -p 27017:27017  bound the container’s port 27017 to 27017  of the host, and –name=mongo-example will give a name to your container instead of an arbitrary name.

Step 3: Verify the state.

docker ps 


Verifying-state

As you can see the container is running successfully.

Step 4: Access the MongoDB shell in the running container

docker exec -it mongo-example mongo


Accessing-MongoDB-shell

As you can see we can access the interactive shell.

What Are The Benefits of Using Docker?

  1. Platform Independent: Docker containers are platform independent means the Docker image can build in one environment and it can be run in the other environment. It will be consistent across all the environments you are going to run the image as a container.
  2. Light Weight: Docker Containers are light in weight when compared to the Virtualization deployment so they can easy to maintain occupies less space.
  3. Easy Scaling: In virtualization deployment, if you want to scale then you need to entire virtual machine but in Docker containers you just need to scale the containers that can be automated depending on the incoming traffic.
  4. Load Balancing: The load balancing can be done across two containers depending on the load the application is receiving and also you can divert the traffic to the multiple containers y depending on the amount of traffic it is receiving.
  5. Resource Isolation: You can allocate a specific amount of resources like CPU, Memory, and storage to the applications which will ensure that they will not reach the allocated limit.

Docker And MongoDB Atlas

MongoDB Atlas

MongoDB Atlas is a database service (DBaaS) platform that is managed and taken completely by MongoDB. Where you need to focus merely on your data extraction and data intrusion rest will be taken care of by the MongoDB itself like scaling and load balancing and the underlying infrastructure. To connect to this database the preferred way using the application which is containerized.

The applications which are deployed using the Docker in the form of containers will be very stable across all the enviroments so it is very preferable with the help of data base string you can extract the data from the MongoDB database.

FAQs On Docker MongoDB

1. Can I Use MongoDB In Docker?

Yes you can run MongoDB by using Docker in the form of Containers.

2. Can You Run MongoDB In Kubernetes?

Yes you can run MongoDB in kubernetes in the containers by using the containerd as an run time.



Last Updated : 11 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads