How To Transfer Data Between Docker MongoDB Instance?
How to take mongoDb data dump and restore it from a docker instance to your local machine and vice-versa
Ankit Bansal
Sep 09, 2024 • 2 min read
Introduction
This comprehensive guide walks you through the process of transferring MongoDB data between Docker containers and local instances. Whether you're migrating databases, creating backups, or synchronizing data across different environments, this guide covers all the essential commands and best practices.
Prerequisites
MongoDB Installation: Ensure MongoDB is installed on your local machine
Docker Setup:
Install Docker (Official Documentation)
Install MongoDB Docker Image (MongoDB Documentation)
Setting Up MongoDB Docker Container
Option 1: Basic Setup
docker run -d --name mongo-on-docker -p 27017:27017 \
-e MONGO_INITDB_ROOT_USERNAME=<your-username> \
-e MONGO_INITDB_ROOT_PASSWORD=<your-password> mongo
Note: Replace
<your-username>
and<your-password>
with your credentials
Option 2: Persistent Storage Setup
export MONGODB_VERSION=6.0-ubi8
docker run --name mongodb -d -p 27017:27017 \
-v $(pwd)/data:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=root \
mongodb/mongodb-community-server:$MONGODB_VERSION
Note: You can customize the username and password from 'root'
Step-by-Step Data Transfer Guide
1. Creating Backups from Docker Instance
Access Docker Container
docker exec -it <container-id> /bin/sh
Create MongoDB Dump
mongodump --host <server_host> --port <server_port> \
--username <username> --password <password> \
--db <dbName> --out <backup_folder_path>
Copy Files from Docker to Local Machine
docker cp <container-id>:/<backup_folder_path> <local_folder_path>
2. Restoring Data
Restore to Local MongoDB
mongorestore --host <server_host> --port <server_port> \
--username <username> --password <password> \
--db <dbName> <backup_folder_path> \
--authenticationDatabase admin
Restore to Docker Instance
Copy backup to Docker:
docker cp <local_folder_path> <container-id>:/<backup_folder_path>
Run restore command inside Docker container
3. Direct Data Transfer Outside Docker
Restore Local Data to Docker Instance
docker exec -it <container-id> sh -c 'mongorestore \
--host <server_host> --port <server_port> \
--username <username> --password <password> \
--db <dbName> <backup_folder_path> \
--authenticationDatabase admin' < <local_folder_path>
Useful Docker Commands
Container Management
# List running containers
docker ps
# Start container
docker start <container-id>
# Stop container
docker stop <container-id>
Best Practices and Tips
Always verify your backups before performing major data operations
Use meaningful names for backup folders and containers
Keep track of your container IDs and credentials
Consider using environment variables for sensitive information
Regularly clean up old backups to manage disk space
Test restore operations in a non-production environment first
Conclusion
Successfully managing MongoDB data between Docker containers and local instances requires careful attention to detail and proper command execution. This guide provides the foundation for handling various data migration scenarios, but remember to always adapt these commands to your specific needs and security requirements.
For production environments, consider implementing additional security measures and backup verification steps. Keep your MongoDB and Docker versions updated, and always test migration procedures in a staging environment before applying them to production systems.
Remember to consult the official MongoDB and Docker documentation for the most up-to-date information and best practices for your specific versions and use cases.