DEV Community

gourab yousuf basir
gourab yousuf basir

Posted on

Installing MongoDB Using Docker: A Complete Guide

In today's fast-paced development environment, containerization has become the go-to solution for deploying databases and services. MongoDB, one of the most popular NoSQL databases, can be easily deployed using Docker. This guide walks you through setting up a MongoDB instance with Docker, complete with persistence, backup capabilities, and remote connection options.

Prerequisites

Before we start, make sure you have:

  • A VPS or server running Linux (Ubuntu, Debian, CentOS, etc.)
  • Docker installed
  • Basic knowledge of terminal commands
  • SSH access to your server

Step 1: Pull the MongoDB Image

The first step is to pull the official MongoDB image from Docker Hub:

docker pull mongo:latest
Enter fullscreen mode Exit fullscreen mode

This command downloads the latest MongoDB image. If you need a specific version, replace latest with the version number (e.g., mongo:6.0).

Step 2: Create Directories for Data Persistence

MongoDB needs a place to store its data. Let's create the necessary directories:

mkdir -p ~/mongodb_data
mkdir -p ~/mongodb_backup
Enter fullscreen mode Exit fullscreen mode

These directories will be mounted as volumes in our Docker container, ensuring data persistence and making backups possible.

Step 3: Run MongoDB Container

Now, let's run MongoDB with all the necessary options:

docker run -d \
  --name mongodb \
  -p 27017:27017 \
  -v ~/mongodb_data:/data/db \
  -v ~/mongodb_backup:/backup \
  -e MONGO_INITDB_ROOT_USERNAME=admin \
  -e MONGO_INITDB_ROOT_PASSWORD=your_secure_password \
  --restart always \
  mongo:latest
Enter fullscreen mode Exit fullscreen mode

Let's break down this command:

  • -d: Runs the container in detached mode (in the background)
  • --name mongodb: Names the container "mongodb" for easy reference
  • -p 27017:27017: Maps the container's port 27017 to the host's port 27017
  • -v ~/mongodb_data:/data/db: Mounts the host directory for data persistence
  • -v ~/mongodb_backup:/backup: Mounts a directory for backups
  • -e MONGO_INITDB_ROOT_USERNAME=admin: Sets the root username
  • -e MONGO_INITDB_ROOT_PASSWORD=your_secure_password: Sets the root password
  • --restart always: Ensures the container restarts automatically after system reboots
  • mongo:latest: Specifies the image to use

Step 4: Verify the Installation

To verify that MongoDB is running correctly:

docker ps
Enter fullscreen mode Exit fullscreen mode

You should see your MongoDB container in the list of running containers.

Step 5: Connect to MongoDB

You can connect to your MongoDB instance using the mongo shell:

docker exec -it mongodb mongosh --username admin --password your_secure_password
Enter fullscreen mode Exit fullscreen mode

If you're connecting remotely, you can use the MongoDB URI format:

mongodb://admin:your_secure_password@your_server_ip:27017
Enter fullscreen mode Exit fullscreen mode

Step 6: Creating Backups

To create a backup of your MongoDB databases:

docker exec mongodb mongodump --username admin --password your_secure_password --out /backup/$(date +%Y%m%d)
Enter fullscreen mode Exit fullscreen mode

This creates a backup with the current date as the folder name.

To restore from a backup:

docker exec -it mongodb mongorestore --username admin --password your_secure_password /backup/20240315/
Enter fullscreen mode Exit fullscreen mode

Step 7: Security Recommendations

  1. Firewall Configuration: Limit access to port 27017 only to trusted IPs
   ufw allow from trusted_ip_address to any port 27017
Enter fullscreen mode Exit fullscreen mode
  1. Use Strong Passwords: Avoid simple passwords for your MongoDB instances

  2. Regular Backups: Set up a cron job to automate your backups

   crontab -e
   # Add this line to backup daily at 2 AM
   0 2 * * * docker exec mongodb mongodump --username admin --password your_secure_password --out /backup/$(date +%Y%m%d)
Enter fullscreen mode Exit fullscreen mode
  1. Consider Using a VPN: For additional security, consider setting up a VPN for database access

Conclusion

Running MongoDB in Docker provides flexibility, easy upgrades, and simple deployment. By following this guide, you've set up a MongoDB instance with data persistence, backup capabilities, and remote access options.

Whether you're developing a small application or a large-scale service, this containerized approach offers a solid foundation for your database needs.

Happy coding!


Note: Remember to replace placeholder values like your_secure_password and your_server_ip with your actual values before running the commands.

API Trace View

How I Cut 22.3 Seconds Off an API Call with Sentry 🕒

Struggling with slow API calls? Dan Mindru walks through how he used Sentry's new Trace View feature to shave off 22.3 seconds from an API call.

Get a practical walkthrough of how to identify bottlenecks, split tasks into multiple parallel tasks, identify slow AI model calls, and more.

Read more →

Top comments (0)

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay