
This guide will walk you through setting up MongoDB in a Docker container with persistent data storage using Docker volumes. We’ll create a dedicated directory structure and configure port mapping for easy access.
Table of Contents
Open Table of Contents
Prerequisites
Before starting, ensure you have:
- Docker installed on your system
- Basic familiarity with terminal/command prompt
- Administrative privileges on your machine
Directory Setup
First, let’s create a dedicated directory structure for our MongoDB instance:
# Create parent directory
mkdir ~/mongodb-docker
# Create data directory for persistence
mkdir ~/mongodb-docker/data
# Navigate to the MongoDB directory
cd ~/mongodb-docker
Docker Configuration
Create a docker-compose.yml file in the mongodb-docker directory:
version: '3.8'
services:
mongodb:
image: mongo:latest
container_name: mongodb
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
- MONGO_INITDB_ROOT_PASSWORD=password123
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db
restart: unless-stopped
volumes:
mongo-data:
driver: local
Let’s break down the configuration:
image: Uses the latest official MongoDB Community Server imageports: Maps container port 27017 to host port 27017volumes: Maps the local./datadirectory to MongoDB’s/data/dbdirectoryenvironment: Sets up initial root username and passwordrestart: Automatically restarts the container unless manually stopped
Running MongoDB
Start the MongoDB container using Docker Compose:
docker-compose up -d
The -d flag runs the container in detached mode (background).
Verifying the Setup
- Check if the container is running:
docker ps
- Verify the volume mounting:
docker volume ls
- Connect to MongoDB using the MongoDB shell:
docker exec -it mongodb mongosh -u admin -p password123
You should now see the MongoDB shell prompt. Try some basic commands:
// Show databases
show dbs
// Create a test database
use test_db
// Insert a test document
db.test_db.insertOne({ message: "Hello Docker MongoDB!" })
// Query the document
db.test.find()
Directory Structure
After setup, your directory structure should look like this:
~/mongodb-docker/
├── docker-compose.yml
└── data/
└── ... (MongoDB data files)
Important Notes
-
Data Persistence: All your MongoDB data will be stored in the
~/mongodb-docker/datadirectory. This ensures your data survives container restarts or removals. -
Security:
- Change the default username and password in production
- Consider adding network restrictions
- Never expose MongoDB directly to the internet
-
Backup: You can backup your data by copying the contents of the data directory. Just ensure MongoDB is stopped first:
docker-compose down
# Now you can safely backup the data directory
docker-compose up -d
Now you have a fully functional MongoDB instance running in Docker with persistent storage! The database will be accessible on localhost:27017 using the credentials specified in the docker-compose file.