Article Image
read

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 ."
Blog Logo

Christopher LaPointe


Published

Interested in Related Posts from this Site?

Virtual PDF Network Printer

May 04, 2023: For my [paperless-ngx](https://github.com/paperless-ngx/paperless-ngx) install, I wanted to be able to easily "print" to it from...

Set up your own mailserver with Maddy

February 10, 2022: About a year ago I wanted to expand beyond namecheap's simple "email forwarding" service and...

Extracting a Certificate from Traefik/acme.json

February 04, 2022: It's a fairly common need to extract a certificate and key from traefik to use...

Docker Samba / CIFS Volume (Photoview)

July 12, 2021: Recently I had a need to mount a CIFS volume as a docker volume: The...

Self-Hosted Analytics: Docker & Privacy-First

July 02, 2021: I have a lot of little sites. Probably too many. Includes various personal sites, sites...

Docker Swarm Registry and Auto Garbage Collection on NFS

March 15, 2021: After moving Traefik to v2, I also updated the common registry infrastructure. Namely, this stack...

Image

Chris LaPointe

Another site of Code

Back to Overview