Skip to content

Instantly share code, notes, and snippets.

@naviocean
Last active March 16, 2021 09:42
Show Gist options
  • Save naviocean/8edc85738032845f83447b8f03292cc9 to your computer and use it in GitHub Desktop.
Save naviocean/8edc85738032845f83447b8f03292cc9 to your computer and use it in GitHub Desktop.
mongodump and mongorestore with Docker

With Docker

With docker, pseudo-tty allocation is deactivated by default, but the -i (interactive) option is required with the restore command.

mongodump

  • No Auth : docker exec <mongodb container> sh -c 'mongodump --archive' > db.dump

  • Authenticated : docker exec <mongodb container> sh -c 'mongodump -d <database> -u <user> -p <password> --archive' > db.dump

mongorestore

  • No Auth : docker exec -i <mongodb container> sh -c 'mongorestore --archive' < db.dump

  • Authenticated : docker exec <mongodb container> sh -c 'mongorestore -d <database> -u <user> -p <password> --archive' < db.dump

With Docker Compose

With docker-compose, pseudo-tty allocation needs to be deactivated explicitly each time with -T :

mongodump

docker-compose exec -T <mongodb service> sh -c 'mongodump --archive' > db.dump

mongorestore

docker-compose exec -T <mongodb service> sh -c 'mongorestore --archive' < db.dump

That's all ! ✨🎉 Now, feel free to write a script and add it to your pipeline.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment