Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker backup and restore mongodb

Tags:

docker

mongodb

Create data only container:

docker create -v /mongodb_data --name mongodb_data mongo

Create monogdb container:

docker run --volumes-from mongodb_data --name mongo_db --restart always -d mongo

Then to use mongodb in my own container I use the link command:

--link mongo_db:mongo

So everything works fine. Now I want to backup the mongodb according to the docs: http://docs.docker.com/userguide/dockervolumes/#backup-restore-or-migrate-data-volumes and this command:

docker run --volumes-from mongodb_data -v $(pwd):/backup busybox tar cvf /backup/backup.tar /mongodb_data

However the created tar file has just an empty /mongodb_data folder. The folder contains not a single file.

Any ideas whats wrong? I am using docker 1.7 on ubuntu 14.04 LTS.

like image 806
UpCat Avatar asked Jul 12 '15 16:07

UpCat


People also ask

Can I use MongoDB in Docker?

MongoDB can be run in a Docker container. There is an official image available on Docker Hub containing the MongoDB community edition, used in development environments. For production, you may custom-build a container with MongoDB's enterprise version.

What is the command to create backup and restore the MongoDB?

The Mongodump command dumps a backup of the database into the “. bson” format, and this can be restored by providing the logical statements found in the dump file to the databases. The Mongorestore command is used to restore the dump files created by Mongodump.


1 Answers

The problem is your data only container. You make your volume with path /mongodb_data which doesn't store any mongo db data. By default, the mongo db storage path is /data/db (according to this #Where to Store Data Section)

As the result, your mongodb data is not saved in your data only container. So here is a workaround:

  • copy /data/db to /mongodb_data in your mongodb container docker exec -it mongo_db bash then cp -r /data/db/* /mongodb_data/

  • make a backup by following the doc you mentioned

  • build a new data only container and load the backup

  • remove current mongo_db container and recreate a new one with the new data only container

OR, you can modify your mongodb config, to change the default directory to /mongodb_data once you copied all data from /data/db to /mongodb_data. You may find this useful Changing MongoDB data store directory

like image 70
Freeznet Avatar answered Oct 20 '22 21:10

Freeznet