MongoDB Backup & Restore with Docker
This guide outlines the process for backing up and restoring MongoDB databases running in Docker containers. It uses generic names for containers and databases, which you should replace with your specific values.
1. Identify Your MongoDB Container
First, list your running Docker containers to identify your MongoDB instance:
docker ps
Identify your MongoDB container (e.g., my-mongo) and any other relevant database containers.
2. Prepare a Backup Directory on the Host
Create a dedicated folder on your host machine where all backups will be stored:
sudo mkdir -p /databases
sudo chown -R $USER:$USER /databases
cd /databases
3. Dump the Database from the Container
Use mongodump to export your database from the running container.
Always use proper authentication.
docker exec my-mongo mongodump \
--username <DB_USER> \
--password <DB_PASSWORD> \
--authenticationDatabase admin \
--db <SOURCE_DB_NAME> \
--out /dump
After dumping, copy the backup from the container to your host machine:
docker cp my-mongo:/dump /databases/mongo-backup-$(date +%F)
Example backup folder structure:
/databases/mongo-backup-2026-01-05/
├── admin/
├── prelude.json
└── <SOURCE_DB_NAME>/
├── collection1.bson
├── collection1.metadata.json
└── collection2.bson
└── ...
4. Start a Fresh MongoDB Container (If Needed)
If you're restoring to a new or clean container, stop and remove the old one, then start a new one:
docker stop my-mongo
docker rm my-mongo
docker run -d \
--name my-mongo \
-p 127.0.0.1:27017:27017 \
-v mongo-data:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=<DB_USER> \
-e MONGO_INITDB_ROOT_PASSWORD=<DB_PASSWORD> \
-e MONGO_INITDB_DATABASE=<SOURCE_DB_NAME> \
mongo:<VERSION>
Verify the MongoDB version inside the new container:
docker exec my-mongo mongosh -u <DB_USER> -p <DB_PASSWORD> --authenticationDatabase admin --eval "db.version()"
5. Copy the Backup into the Container for Restore
Create a directory inside the container to hold the backup files, then copy them over:
docker exec my-mongo mkdir -p /backup
docker cp /databases/mongo-backup-2026-01-05/<SOURCE_DB_NAME> my-mongo:/backup/<SOURCE_DB_NAME>
Verify the files were copied:
docker exec my-mongo ls -F /backup/<SOURCE_DB_NAME>
6. Restore the Database (to the Same or a New Name)
Use mongorestore to import the data. You can restore it to the
original database name or a new one using the --db flag:
docker exec my-mongo mongorestore \
--username <DB_USER> \
--password <DB_PASSWORD> \
--authenticationDatabase admin \
--db <RESTORE_DB_NAME> \ # Use <SOURCE_DB_NAME> to restore to the original name
/backup/<SOURCE_DB_NAME>
Success output example:
X document(s) restored successfully. 0 document(s) failed to restore.
Verify in mongosh:
docker exec -it my-mongo mongosh -u <DB_USER> -p <DB_PASSWORD> --authenticationDatabase admin
Once inside mongosh:
> show databases;
> use <RESTORE_DB_NAME>;
> show collections;
> db.<collection_name>.findOne();
7. Important Notes & Caveats
- Authentication: Always use full authentication (
--username,--password,--authenticationDatabase) formongodumpandmongorestoreinside Docker. - Container Paths: Ensure the
/dumpor/backupdirectory exists inside the container before attempting to copy files to it. - Metadata Files: The
*.metadata.jsonfiles are crucial as they contain index definitions and other collection metadata. Always include them when restoring. - Selective Renames: For advanced scenarios where you need to restore specific collections and rename them, use
mongorestore's namespace options:--nsInclude <namespace_pattern>--nsFrom <original_namespace>--nsTo <new_namespace>
--nsFrom "source_db.collection1" --nsTo "target_db.new_collection1") - MongoDB Version: Ensure the
mongo:<VERSION>tag matches the version of MongoDB you intend to use.
Command Reference
| Command | Purpose / What it Does | Notes / Context |
|---|---|---|
docker ps | List running containers | Check status of containers |
docker exec -it my-db mongosh -u admin -p admin123 --authenticationDatabase admin | Access MongoDB shell inside container | View databases and perform operations |
docker run -d --name my-db -p 127.0.0.1:27017:27017 -v my-db-volume:/data/db my-db-image | Start a new container from backup image | Run MongoDB or other DB container on target machine |
docker cp ~/backup/my-db-dump my-db:/tmp/my-db-dump | Copy database dump into container | Copy to a safe location inside container (not volume mount) |
docker commit <container-id> my-db-backup | Save running container as an image | Create reusable backup image of container |
docker save -o ~/backup/my-db-backup.tar my-db-backup | Save Docker image to tar file | For transferring image to another machine |
docker load -i ~/backup/my-db-backup.tar | Load saved Docker image on target machine | Makes image available for running containers |
docker run --rm -v my-db-volume:/data/db -v ~/backup:/backup ubuntu tar czvf /backup/my-db-volume-backup.tar.gz -C /data/db . | Backup Docker volume | Use temporary container to archive DB volume |
docker run --rm -v my-db-volume:/data/db -v ~/backup:/backup ubuntu bash -c "cd /data/db && tar xzvf /backup/my-db-volume-backup.tar.gz --strip 1" | Restore Docker volume backup | Restore data from tar to Docker volume |
docker exec -it my-db mongorestore -u admin -p admin123 --authenticationDatabase admin /tmp/my-db-dump | Restore MongoDB database from dump | Correct restore method when DB uses a volume |