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

Command Reference

CommandPurpose / What it DoesNotes / Context
docker psList running containersCheck status of containers
docker exec -it my-db mongosh -u admin -p admin123 --authenticationDatabase adminAccess MongoDB shell inside containerView 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-imageStart a new container from backup imageRun MongoDB or other DB container on target machine
docker cp ~/backup/my-db-dump my-db:/tmp/my-db-dumpCopy database dump into containerCopy to a safe location inside container (not volume mount)
docker commit <container-id> my-db-backupSave running container as an imageCreate reusable backup image of container
docker save -o ~/backup/my-db-backup.tar my-db-backupSave Docker image to tar fileFor transferring image to another machine
docker load -i ~/backup/my-db-backup.tarLoad saved Docker image on target machineMakes 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 volumeUse 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 backupRestore data from tar to Docker volume
docker exec -it my-db mongorestore -u admin -p admin123 --authenticationDatabase admin /tmp/my-db-dumpRestore MongoDB database from dumpCorrect restore method when DB uses a volume

Related Articles

A.M. Rinas

Written by A.M. Rinas

Full-Stack Software Engineer working on web applications, DevOps automation, and databases. More about me on the about page, or say hi through the contact page and LinkedIn.

← Back to all articles