Backup & Restore Docker Named Volumes

Jarek Lipski
Mar 13, 2019 · 2 min read
Image for post
Image for post

When I was looking for a way to back up Docker named volumes I was surprised to find out that there’s no standard way of handling the process. In the official documentation there’s only a note about using data volume containers and a --volumes-from option. There’s also a docker cp command, but it requires the volume path inside the container that uses them, which makes it less generic.

After a bit of research, it turned out it’s actually pretty easy to back up volumes using volume mounts and a tar utility. For example to backup some_volume to /tmp/some_archive.tar.bz2, you can simply run:

docker run --rm -v some_volume:/volume -v /tmp:/backup alpine tar -cjf /backup/some_archive.tar.bz2 -C /volume ./

And to restore it, just run:

docker run --rm -v some_volume:/volume -v /tmp:/backup alpine sh -c "rm -rf /volume/* /volume/..?* /volume/.[!.]* ; tar -C /volume/ -xjf /backup/some_archive.tar.bz2"

Note: As a safety precaution, I advise you to stop all the containers using the volume being backed-up or restored, otherwise an inconsistent / intermediate state might be archived or recovered.

In the end I wrote my own little volume-backup utility that simplifies the backup & restore process even further and offers some additional benefits, such as using stdout and stdin streams.

Here’s the example backup command:

docker run --rm -v some_volume:/volume -v /tmp:/backup loomchild/volume-backup backup some_archive

And here’s an example restore command:

docker run --rm -v some_volume:/volume -v /tmp:/backup loomchild/volume-backup restore some_archive

Feel free to check out the project page on GitHub for complete documentation and let me know what you think.

Edit: Changed the cleanup code to delete hidden files — thanks Olivier.

Edit: It’s also possible to backup to standard output and restore from standard input. I added this capability to volume-backup — thanks Holger.

Edit: Added –rm flag to remove the container when finished — thanks awade.

Originally published at on March 26, 2017.

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store