1 | initial version |
Start a new MongoDB container with the same version as the original container.
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.
docker exec -it new-mongo /bin/bash
mongodump --dbpath /data/db --out /backup
Here, /backup
is the location where the backup will be stored inside the container.
Once the backup is complete, exit the shell.
Copy the backup from the container to a local directory using the docker cp command:
docker cp new-mongo:/backup /path/to/local/backup
docker stop new-mongo
docker rm new-mongo
Start a new MongoDB container using the same command as in step 2, but this time add the --auth flag to require authentication.
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.