Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
  1. Start a new MongoDB container with the same version as the original container.

  2. Mount the external volume to the new container using the -v flag in the docker run command. For example:

docker run -d --name new-mongo -v my_volume:/data/db mongo

Note that my_volume is the name of the volume holding the MongoDB data.

  1. Once the new container is running, open a shell in the container using the docker exec command:
docker exec -it new-mongo /bin/bash
  1. Use the mongodump command to create a backup of the data in the mounted volume:
mongodump --dbpath /data/db --out /backup

Here, /backup is the location where the backup will be stored inside the container.

  1. Once the backup is complete, exit the shell.

  2. Copy the backup from the container to a local directory using the docker cp command:

docker cp new-mongo:/backup /path/to/local/backup
  1. Stop and remove the new container:
docker stop new-mongo
docker rm new-mongo
  1. Start a new MongoDB container using the same command as in step 2, but this time add the --auth flag to require authentication.

  2. Use the mongorestore command to restore the data from the backup:

mongorestore --dbpath /data/db --drop /path/to/local/backup

Here, /path/to/local/backup is the location of the backup file on the host machine.

  1. Verify that the data has been successfully restored by connecting to the MongoDB instance and checking the databases and collections.