Peerlist Blog

engineering

How To Transfer Data Between Docker MongoDB Instance?

How To Transfer Data Between Docker MongoDB Instance?

MongoDB data dump and restore from docker instance to local machine and vice-versa.

2 min read

Ankit Bansal

Sep 03, 2023

Introduction:

You can have a look at this article where we tried to import and export data from the docker instance into the local MongoDB instance.

Prerequisites:

  • MongoDB Installed: Ensure you have MongoDB installed and set up on your local machine.
    • Install Docker. Documentation: https://docs.docker.com/get-docker/

    • Mongodb Docker Installation: https://www.mongodb.com/docs/manual/tutorial/install-mongodb-community-with-docker/

      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
      
    • I hope you replace your username with <your-username> and password <your-password>

    • Or Docker with permanent db

      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
      
    • You can replace username password root root to anything you like.

    • Save the docker instance ID somewhere.

Step-by-Step Guide:

  • Taking a Backup/Dump from the Docker Instance:

    1. Get into the docker instance
        docker exec -it <dockerID that we received from above command>  /bin/sh
      
      • for eg docker exec -it f30586c864b4b8cb4b21ca897e716746683840a9072389769b21be346ea4a468 /bin/sh
    2. Command for taking a dump
      mongodump --host <server_host> --port <server_port> --username <username> --password <password> --db <dbName> --out <backup_folder_path>
      
      • For eg: mongodump --host <hostname> --port <27017> --username pladmin --db peerlist --password root "./bkup" --authenticationDatabase admin
    3. Copying files from docker to Local machine
        docker cp <docker-Id-f30586c>:/<backup_folder_path> <local_folder_path>
      
      • For eg: docker cp <docker-Id-f30586c>:/bkup ~/Downloads/bkup
    4. Restoring to the local machine
      mongorestore --host <server_host> --port <server_port> --username <username> --password <password> --db <dbName> <backup_folder_path> --authenticationDatabase admin
      
      • For eg: mongorestore --host localhost --port 27017 --username pladmin --db peerlist --password root "./bkup/peerlist" --authenticationDatabase admin
    5. Similarly Restoring to the Docker instance
      • First, take the dump similar to the above command
      • Copy the dump/backup to docker via docker cp <local_folder_path> <docker-Id>:/<backup_folder_path>
      • Run the same restore command at point 4
  • Transfer Backup/Dump from outside the Docker Instance:

    1. Restoring local data in a docker instance
    docker exec -it <docker-Id> sh -c 'mongorestore --host <server_host> --port <server_port> --username <username> --password <password> --db <dbName> <backup_folder_path> --authenticationDatabase admin' < <local_folder_path>
    
    • For eg restoring separate collection
    docker exec -it f30586c864b4b8cb4b21ca897e716746683840a9072389769b21be346ea4a468 sh -c 'mongorestore --host localhost --port 27017 --username pladmin --db peerlist --password root --collection prof-ana' < ~/Downloads/profile-analytics.json
    
  • Command to start a docker with docker id

    docker start bf1e4cc19f0abc88e4243891a6c08d13e7e874aeb87a47e0ba181a21d2dcdff9
    
  • Command to stop a docker with docker id

    docker stop bf1e4cc19f0abc88e4243891a6c08d13e7e874aeb87a47e0ba181a21d2dcdff9
    
  • **To check all docker running instances **

    docker ps