Migrating MongoDB data between Docker containers can be efficiently accomplished using the `mongodump` and `mongorestore` utilities. This guide walks you through the process of backing up a MongoDB database from one Docker container and restoring it to another, utilizing gzip compression for efficient storage.
Prerequisites
-
Docker installed on your machine.
-
Two running MongoDB Docker containers (source and destination):
Image Container Port mongodb/mongodb-community-server:6.0.5-ubuntu2204 mongo-source 27017 mongodb/mongodb-atlas-local:latest mongo-dest 27018 -
Basic knowledge of Docker and MongoDB commands.
Pull the MongoDB Docker Images
If you haven't already, pull the MongoDB images for source container first:
docker pull mongodb/mongodb-community-server:6.0.5-ubuntu2204
Then, pull the MongoDB image for the destination container:
docker pull mongodb/mongodb-atlas-local:latest
Run the MongoDB Containers
Start the source MongoDB container:
docker run -d --name mongo-source -p 27017:27017 mongodb/mongodb-community-server:6.0.5-ubuntu2204
Start the destination MongoDB container:
docker run -d --name mongo-dest -p 27018:27017 mongodb/mongodb-atlas-local:latest
Insert Sample Data into the Source Container
To demonstrate the migration process, let's insert some sample data into the source MongoDB container.
First, access the MongoDB shell inside the source container:
docker exec -it mongo-source mongosh
Once inside the container, open the MongoDB shell and select your database:
use company
Insert sample documents into a collection:
db.employees.insertMany([
{
_id: ObjectId("6927c8b71cc417a97674e39a"),
name: "Budi Santoso",
role: "Pengembang Backend Senior",
department: "Teknologi Informasi",
location: "Jakarta Selatan",
employment_status: "Karyawan Tetap",
},
{
_id: ObjectId("6927c8b71cc417a97674e39b"),
name: "Siti Aminah",
role: "Desainer UI/UX",
department: "Produk & Desain",
location: "Bandung",
employment_status: "Kontrak",
},
{
_id: ObjectId("6927c8b71cc417a97674e39c"),
name: "Andi Pratama",
role: "Manajer Produk",
department: "Manajemen Produk",
location: "Surabaya",
employment_status: "Karyawan Tetap",
},
]);
Exit the MongoDB shell:
exit
Backup the MongoDB Database from the Source Container
Use `mongodump` with gzip compression to back up the database:
docker exec mongo-source sh -c "mongodump --archive=/tmp/company_backup.gzip --gzip --db=company"
This creates a compressed backup stored in the `/tmp` directory of the source container.
Copy the Backup File to the Destination Container
Copy the backup file from the source container to your host:
docker cp mongo-source:/tmp/company_backup.gzip ./company_backup.gzip
This places the backup in your current directory on the host machine.
Now, copy the backup file from your host to the destination container:
docker cp ./company_backup.gzip mongo-dest:/tmp/company_backup.gzip
Restore the Backup to the Destination Container
Use `mongorestore` with gzip decompression to restore the database:
docker exec mongo-dest sh -c "mongorestore --archive=/tmp/company_backup.gzip --gzip"
This command restores the `company` database from the backup file.
Verify the Data in the Destination Container
Access the MongoDB shell inside the destination container:
docker exec -it mongo-dest mongosh
Once inside the container, switch to the restored database:
use company
Check the documents in the `employees` collection:
db.employees.find().pretty();
End the MongoDB shell session:
exit
You should see the same documents that were inserted into the source container.
Cleanup
After verifying the data, you can stop and remove the Docker containers if they are no longer needed:
docker stop mongo-source mongo-dest
docker rm mongo-source mongo-dest
Conclusion
Migrating MongoDB data between Docker containers using `mongodump` and `mongorestore` with gzip compression is a straightforward process. This method ensures that your data is efficiently backed up and restored, making it easy to transfer databases between different MongoDB instances running in Docker containers.