MongoDB in Docker Container: A Comprehensive Guide

MongoDB in Docker Container

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

As a container-based deployment became an industry standard in our fast-paced developing world. So, this is how to run a MongoDB in Docker container, which ensures the management of the databases in a very easy way with portability, scalability, and low maintenance. The same thing, running the MongoDB in Docker is preferable by developers and system administrators because it removes system dependency issues and runs a consistent environment across various different platforms.

You can even use a MongoDB container, which helps you fire an actual MongoDB instance in seconds, so you can manage your database without installing MongoDB on your local machine or server. If you are developing locally or deploying in a production environment, containerized databases have made the scaling and backup processes far simpler.

Let’s learn together!

Deploying MongoDB in a Docker Container

Step 1: Install Docker

Before we go ahead, make sure you have Docker installed on your machine. You should run to verify that Docker is installed:

docker --version

Output:

Docker version 24.0.5, build 1234567

To install Docker, follow our guide to install Docker.

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

Step 2: Pull MongoDB Docker Image

Run the below command to pull the latest official MongoDB image.

docker pull mongo:latest 

Output:

latest: Pulling from library/mongo Digest: sha256:xxxxxxx Status: Downloaded newer image for mongo:latest

This command pulls the latest MongoDB container image from Docker Hub.

Step 3: Run MongoDB Container

Let’s start by creating and running a MongoDB container with persistent storage:

docker run -d \ --name mongodb-container \ -p 27017:27017 \ -e MONGO_INITDB_ROOT_USERNAME=admin \ -e MONGO_INITDB_ROOT_PASSWORD=password \ -v mongodb_data:/data/db \ mongo:latest

Explanation:

  • -d : Start a container in detached mode
  • –name mongodb-container: Set a name to the container
  • -p 27017:27017: Map MongoDB’s default port to the host machine
  • -e MONGO_INITDB_ROOT_USERNAME=admin: We set root username
  • -e MONGO_INITDB_ROOT_PASSWORD=password: Root password
  • -v mongodb_data:/data/db: Creates a persistent volume for the MongoDB data

Run the following command to confirm MongoDB is running:

docker ps

Output:

CONTAINER ID IMAGE STATUS PORTS NAMES a1b2c3d4e5f6 mongo:latest Up 10 minutes 0.0.0.0:27017->27017/tcp mongodb-container 

Step 4: Access MongoDB in Docker Container

Use the command to get the MongoDB shell inside the running container:

docker exec -it mongodb-container mongosh 

If successful, you must see the following:

test>

You can now work with MongoDB in a similar way to how you would work with it in a full database setup.

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

Managing MongoDB Container with Docker Compose

Deploying MongoDB with Docker Compose Create a docker-compose.yml file:

version: '3.8' services: mongodb: image: mongo:latest container_name: mongodb-container ports: - "27017:27017" environment: MONGO_INITDB_ROOT_USERNAME: admin MONGO_INITDB_ROOT_PASSWORD: password volumes: - mongodb_data:/data/db volumes: mongodb_data: 

Run MongoDB with:

docker-compose up -d

This allows MongoDB to start automatically with the proper configurations.

How to Stop and Remove the MongoDB Container

To stop MongoDB:

docker stop mongodb-container

To uninstall MongoDB and its data volume:

docker rm -v mongodb-container

How to Manage MongoDB Containers Using CyberPanel

CyberPanel

CyberPanel is a powerful web hosting control panel that simplifies Docker management. Here’s what CyberPanel does better with MongoDB in Docker:

  • Docker Management at One-Click: CyberPanel has a GUI that allows anyone to deploy and manage MongoDB containers.
  • Automated Backups: For your MongoDB, you can perform backups automatically to avoid data loss.
  • Container Monitoring: View logs, resource usage, and container status directly from the CyberPanel dashboard
  • Security and firewall management: Use iptables or similar tools to restrict unauthorized MongoDB access.
  • Database Management Made Easy: With CyberPanel, you can set up and manage databases right through the integrated tools.

The integration of CyberPanel with MongoDB would enable you to manage databases and scale applications effectively.

FAQs: MongoDB in Docker Container

1. Why would you want to run MongoDB in a Docker container?

Running MongoDB in Docker container ensures portability, easy deployment, and isolation from the host system.

2. What is the role of Docker in organizing MongoDB instances?

Using Docker for MongoDB allows for quick and painless setup, scaling, and containerized environments that can be easily replicated.

3. Can I run multiple MongoDB containers on the same host?

Yes, but each container must expose a unique port mapping or run on a different network.

4. Does MongoDB in Docker lose data after a restart?

Data is removed when the container is removed (default behaviour). Docker volumes keep the data when starting containers up and down.

Conclusion: Using Docker to Deploy MongoDB Easily

If you’re looking to run MongoDB in a Docker container, Docker makes the process of setting up a database much easier and more manageable, as it also returns a consistent, scalable, and portable environment for the application. By implementing MongoDB in a containerized environment, you can improve efficiency and minimize setup complexity whether Docker Compose is your tool of choice for automation or CyberPanel provides advanced control over your DB.

Ready to deploy MongoDB effortlessly? Start using MongoDB in Docker today and optimize your development workflow

Hasib Iftikhar
I'm Hasib Iftikhar, a dedicated technical writer at CyberPanel, joining the team in July 2024. With three years of extensive experience in content writing, I specialize in copywriting, article writing, guest posting, affiliate content writing, and SEO. My expertise ensures that each piece of content I create is engaging, informative, and optimized for search engines, helping businesses enhance their online presence and reach their target audience effectively.
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!