engineering

How To Transfer Data Between Docker MongoDB Instance?

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

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

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

  1. Access Docker Container

docker exec -it <container-id> /bin/sh
  1. Create MongoDB Dump

mongodump --host <server_host> --port <server_port> \
  --username <username> --password <password> \
  --db <dbName> --out <backup_folder_path>
  1. Copy Files from Docker to Local Machine

docker cp <container-id>:/<backup_folder_path> <local_folder_path>

2. Restoring Data

  1. Restore to Local MongoDB

mongorestore --host <server_host> --port <server_port> \
  --username <username> --password <password> \
  --db <dbName> <backup_folder_path> \
  --authenticationDatabase admin
  1. 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

  1. Always verify your backups before performing major data operations

  2. Use meaningful names for backup folders and containers

  3. Keep track of your container IDs and credentials

  4. Consider using environment variables for sensitive information

  5. Regularly clean up old backups to manage disk space

  6. 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.

Create Profile

or continue with email

By clicking "Create Profile“ you agree to our Code of Conduct, Terms of Service and Privacy Policy.