How to Back Up Your Data Volumes to Docker Hub

DZone 's Guide to

How to Back Up Your Data Volumes to Docker Hub

Backing up changes that you've made to the container is pretty simple, but backing them up to the data volume is a little more difficult.

· DevOps Zone ·
Free Resource

So, you went and created a data volume for your container. Now, you would like to back up the changes made to the container as well as the data volume itself. The first part is easy; simply commit and push the image to Docker Hub. But what about the data volume? By definition, it is not committed as part of the image.

Why would one like to commit a data volume? The idea is that if we work on a cluster (coreOs for instance), we do not know where our image is going to run at, and we would like it to have its data volume popup next to it as well.

The Docker tutorial suggests that you can backup and restore the data volume locally. We are going to use this technique and add a few more lines to get this backup pushed into Docker Hub for easy future restoration to any location we desire. So, let's get started. These are the steps to follow:

  1. Back up the data volume from the data container named data-container-to-backup:

docker run --rm --volumes-from data-container-backup --name tmp-backup -v $(pwd):/backup ubuntu tar cvf /backup/backup.tar /folderToBackup

2. Expand this TAR file into a new container so we can commit it as part of its image:

docker run -d -v $(pwd):/backup --name data-backup ubuntu /bin/sh -c "cd / && tar xvf /backup/backup.tar"

3. Commit and push the image with a desired tag ($VERSION):

docker commit data-backup repo/data-backup:$VERSION
docker push repo/data-backup:$VERSION

4. Finally, let's clean up:

docker rm data-backup
docker rmi $(docker images -f "dangling=true" -q)

Now, we have an image named data-backup in our repo that is simply a filesystem with the backed-up files and folders. In order use this image (aka restore from backup), we do the following:

1. Run the data container with the data-backup image:

run -v /folderToBackup --entrypoint "bin/sh" --name data-container repo/data-backup:${VERSION}

 2. Run your image with volumes from the data-container:

docker run --volumes-from=data-container repo/whatEver

That's it!

I was surprised there is no documentation for this workaround. I hope someone finds this helpful. I know it took me a while to think about this.

tutorial ,devops ,docker hub ,data volumes

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}