How to backup and restore Docker volumes

Recently, I had to update a major version of Debian. One of their “before starting updating” steps is to remove all non-Debian installed packages . Docker is one of them. Uninstalling Docker is as simple as $ sudo apt-get purge docker if I was not relying heavily on external volumes.

But, it turns out backup-ing an external volume is very straightforward:

# Backup the output command's path
docker volume inspect {volume} | jq -r ".[].Mountpoint"

# You can use tar for that
tar czf backup.tar.gz {mount-point}

Once you have Docker installed again, all you need to do is copy the backup file to the new Docker volume:

# Create your new volume
docker volume create {volume}

# Get the new volume's path
docker volume inspect {volume} | jq -r ".[].Mountpoint"

# You can use tar for that
tar xf backup.tar.gz --directory={mount-point}