← Back to Blog

2025-02-12

Stop Installing MongoDB Locally - Use Docker Instead

Learn how to run MongoDB inside a Docker container — with port mapping, persistent volumes, and zero installation hassle.

In the world of modern software development, Docker has become a powerhouse tool. Whether you're building microservices, experimenting with new technologies, or setting up a local dev environment, Docker simplifies how you run and manage applications.

One use case that stands out is running databases like MongoDB inside Docker containers. In this article, I'll walk you through how to run MongoDB in Docker — step by step — and explain why this approach is not just convenient but often essential.

Why Use Docker for MongoDB?

  • Isolation: MongoDB runs independently of your system's configuration and other software
  • Portability: Run the same setup across different machines — no "it works on my machine" issues
  • Fast Setup & Teardown: Spin up a MongoDB instance in seconds and remove it just as quickly
  • Data Persistence (with Volumes): Store your data securely even if the container is removed
  • No Installation Hassles: No manual install, no version conflicts

Whether you're a backend developer, DevOps engineer, or just learning MongoDB — Docker gives you agility and control.

Prerequisites

Step 1 — Pull the MongoDB Docker Image

Download the official MongoDB image from Docker Hub:

docker pull mongo

This fetches the latest version of MongoDB and stores it locally.

Step 2 — Run MongoDB in a Container

Start a MongoDB container from the image:

docker run -d --name mongodb-container -p 27017:27017 mongo

What the flags do:

  • -d — runs the container in detached (background) mode
  • --name mongodb-container — gives the container a name
  • -p 27017:27017 — maps your host port 27017 to the container's MongoDB port

MongoDB is now live inside Docker.

Step 3 — Verify the Container is Running

docker ps

You should see mongodb-container listed with its status and port mapping.

Step 4 — Access the Mongo Shell

Enter the MongoDB shell directly inside the container:

docker exec -it mongodb-container mongosh

You'll be dropped into the MongoDB shell, ready to run commands.

Step 5 — Run Some Test Commands

// Create a database
use mydb

// Create a collection
db.createCollection("users")

// Insert a document
db.users.insertOne({ name: "Alice", age: 28 })

// Retrieve the data
db.users.find()

You should see your inserted document in the output. MongoDB is working perfectly inside Docker.

Persisting Data with Docker Volumes

Why This Matters

By default, MongoDB stores data inside the container's filesystem. When the container is deleted, all your data is lost. That's where Docker volumes come in.

Volumes are persistent storage mechanisms managed by Docker, allowing data to survive beyond the lifecycle of any single container.

Step 6 — Create a Docker Volume

docker volume create mongodb_data

Step 7 — Run MongoDB with the Volume Attached

docker run -d \
  --name mongodb-container \
  -p 27017:27017 \
  -v mongodb_data:/data/db \
  mongo

The -v flag maps the named volume to MongoDB's internal data directory (/data/db). Your data now persists even after the container is removed.

Cleaning Up

Stop and remove the container when you're done:

docker stop mongodb-container
docker rm mongodb-container

Remove the volume only if you no longer need the data:

docker volume rm mongodb_data

Summary

Running MongoDB in Docker is a smart, efficient approach for development and testing. It gives you:

  • Clean, isolated environments
  • Portable setups across machines
  • Data safety through volumes
  • Zero installation conflicts

Whether you're prototyping a new app, testing APIs, or teaching MongoDB — Docker lets you focus on building, not configuring.