While managing my docker swarm, it has become apparent that I have a need to upload a set of files to a new or existing docker volume on a regular basis.
I use this, for instance, to upload a new set of alerts to my prometheus node without needing to take it down. This can also be used to backup and restore files to a volume.
The trick is effectively this: Use tar to bundle up files, transmit over a pipe (stdout -> stdin), and extract.
Downloading files to a local tar
Replace MY_VOLUME
as you see fit.
This will mount a volume in read-only mode at /backup
, tar it up, send it over stdin, and then save it to a local file.
docker run --rm -v MY_VOLUME:/backup:ro -w /backup busybox tar cvzf - ./ > MY_VOLUME.tar.gz
Uploading a local directory to a volume
In this case, replace DATAPATH
with the path you want to upload, and MY_VOLUME
with the target.
This will overwrite or create a new file. It will not delete.
# Replace with: "cat myvol.tar.gz" if you want to simply upload a tar file, rather than submitting a new one
# This will also assign global read-write priveleges to the file. Remove the chmod if you don't want that
tar czf - -C DATAPATH ./ | docker run --rm -i -v MY_VOLUME:/restore -w /restore busybox sh -c "tar xvpzf - && chmod -R gou+rwX ."