Backing up docker volumes for beginners

Backing up docker volumes for beginners
Photo by Ian Taylor / Unsplash

Docker volumes are the preferred way of managing persistent data with running docker containers. Some reasons for this include:

  1. Not needing to worry about UID/GIDs between your OS and the system in the docker container.
  2. The data in the docker container becomes isolated from your file system, this limiting the blast-radius of damage via attacks
  3. Volumes are portable between installations of docker without needing to worry about the OS of the host system. This also means that you will be able to use external tools to manage docker volumes (nfs, bucket based storage and others)

If you are just starting out with docker and getting started with storing data on docker volumes, there can be some frustration / anxiety with storing data on docker volumes for multiple reasons:

  • The files not easily visible on your local file system
  • Additional commands that you'll need to learn to copy data out of the container
  • Additional commands to know to grab a shell on a running docker container to inspect what is going on

The additional friction to becoming comfortable with docker containers might be a pain but it is definitely a valuable tool when it comes to partitioning out application data from the code that is needed to run an application.

Once you've started playing with docker and start to have data associated to your containers, you'll want to have tooling to start backing up your volumes and be able to restore them should anything happen. To this effect, I've provided a handy tool if you're working on a unix box to backup and restore docker volumes at our public docker-backup-volume github repository. We hope this code is useful to you!

How to backup your volume

backup-volume.sh will create a new running docker container and mount the volume to the container. A docker cp will be run and the contents of the volume will be saved into a location determined by:

$prefix/$volume/<timestamp>_$volume.tar.gz

An example to run the command is the following:

backup-volume.sh -v <volume_name> -p <prefix>

Restoring your docker volume

Volumes can be restored using create-volume-from-backup.sh by invoking it in the following manner:

create-volume-from-backup.sh -v <volume_name> -p <prefix>

Optionally if you wish to create a volume with a different name from the backup the script can be invoked in the following manner:

create-volume-from-backup.sh -v <volume_name> -p <prefix> -n <new_volume_name>

Backing up volumes and restoration in action

The animation below illustrates how the docker volume owncloud-docker_files is saved to /tmp/backups/owncloud-docker_files. The volume is saved as a zipped tar file with the date and time it was saved.

backing up a docker volume

The following animation shows how the docker volume is restored from /tmp/backups specifying the snapshot file to restore fom. Note: If you wish to restore the docker volume with a different name than the backup, this can be done by adding a -n flag to the command and specifying the new volume name.

restoring a docker volume