How to dump & restore a MongoDB database from a docker container

MongoDB is an interesting tool. Dead simple to create and modify records (or, "documents") that are basically pure JSON goodness, juxtaposed with at times tools or commands that make you scratch your head in utter confusion and/or disbelief.

To help prevent me from having to re-figure this out a few more times, here's a quick how-to on how to back up and restore a MongoDB database, all collections included. The process is unfortunately a little less elegant as compared to PostgreSQL or MariaDB, but hopefully you don't have to do it too often.

How to dump & restore a PostgreSQL database from a docker container
Sometimes you need to quickly dump and restore a PostgreSQL database, but what’s the easiest way to do this when your database is in a Docker container? How to dump & restore a MariaDB/MySQL database from a docker containerIn the same vein as my previous post on dumping and
How to dump & restore a MariaDB/MySQL database from a docker container
In the same vein as my previous post on dumping and restoring your PostgreSQL database, here are mostly copy/paste-able commands for when you’re using a MariaDB or MySQL database. How to dump & restore a PostgreSQL database from a docker containerSometimes you need to quickly dump and restore a

Create a database dump

We can use the following command to create a dump of your entire database, all collections included. Unlike with most other database solutions, the dump will be a bunch of individual files, and so we can't easily rely on shell piping to get this somewhere directly on your docker host machine. Instead we'll simply dump the files to somewhere within the docker container, and then docker cp them out.

❯ docker exec -i <container_name> /usr/bin/mongodump --username <username> --password <password> --authenticationDatabase admin --db <database_name> --out /dump

Now that we have the database dump files, we need a way to get them out of the docker container. Let's copy the files out of the container:

❯ docker cp <container_name>:/dump ~/Downloads/dump

That's it, we finally have our files. Now we can basically do the exact opposite in our new container. Let's do that next.


Restoring

Now that we have the database dump files ready, let's go ahead and import (or, restore) them. Whether this is a fresh and new container or you're restoring a backup, the process is the same.

Ensure a user is created first

Before we continue, we should make sure we have a database user account ready with read and write access to the database you want to restore into. If you don't have one already, let's go ahead and create one now.

You can run the following in your mongodb (cli) client of choice to create a new user. A prompt will appear after running this command in which you can specify the user's password.

use admin

db.createUser({
    user: "username",
	pwd: passwordPrompt(),
	roles:[{role: "readWrite" , db:"<database_name>"}]})

Why "use admin"

One part that seems somewhat confusing and that threw me off previously is that by default when you try to connect to a MongoDB server it expects that admin is the database in which the user exists – even if said user has no access permissions for this database.

There are ways to change this behavior, but that is outside the scope of this guide. As you specify user roles that specify which database(s) the user has actual access to, even though the user might exist in the admin database, it doesn't actually have any permissions to do anything there, so it seems to not be that big of a deal.

Slightly confusing, but that seems to be the Mongo way. Anyway, let's continue.

Copy dump files to container

Just like before, we should first make the dumped database files available within the docker container.

❯ docker cp ~/Downloads/dump <container_name>:/dump

Restore

Now that the dump files are available within the container, run the following command to have everything be imported.

❯ docker exec -i <container_name> /usr/bin/mongorestore --username <username> --password <password> --authenticationDatabase admin --db <database_name> /dump/<database_name>
Note: When mongodump creates a databasae dump, all files are placed in a sub-directory named after the database. That's why in the above example the path points to /dump/<database_name>. You can leave this off if you have a different folder structure.

Cleaning up afterwards

If you'd like to delete the dump files from either docker container after importing, you can open a shell session within the docker container like so:

❯ docker exec -it <container_name> /bin/bash

Now you can delete these files by running something like rm -rf /dump.


That's it! While not as straight-forward as with some other database solutions, it's not too difficult fortunately.

I hope this guide was able to help you. I certainly will be re-visiting this myself once I inevitably end up forgetting how to do this stuff.

Thank you.

Dave Jansen

Dave Jansen

South Korea