Backup MongoDB inside of Docker the right way

Nicco
Nicco
Aug 15 · 2 min read

Backing up a mongo instance is more confusing than it should be. Maybe you have run into a the input device is not a TTY or you simply don't know how to do it? Here are two 1-Liner to backup and restore a running mongo instance.

Photo by Tobias Fischer on Unsplash

Setup

First we define our mongo instance like below. Notice that instead of mapping the data directory onto our filesystem we have a native volume.

docker-compose.yml

version: '3.7'

volumes:
db:

services:
db:
image: mongo:3-xenial
restart: always
volumes:
- db:/data/db
ports:
- 27017:27017

Then start with docker-compose up -d.

Backup

First we will do a backup of our running instance.

docker-compose exec -T db mongodump --archive --gzip --db mydb > dump.gz

The -T option is for enabling piping the output to our own machine. We also tell mongo to use the --gzip option to compress the file significantly.
Lastly we specify the --db <database> that we want to backup.

Restore

Whenever we want to restore a db, or maybe seed it we can run the following:

docker-compose exec -T db mongorestore --archive --gzip < dump.gz

Nicco

Written by

Nicco

Avid coder. https://nicco.io

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade