How do you back up and restore a named volume?
IntermediateConceptVolumes & Bind Mounts
Answer
Use a throwaway container that mounts the volume and a host directory.
docker run --rm -v pgdata:/data -v $PWD:/backup alpine \
tar czf /backup/pgdata.tgz -C /data .
docker run --rm -v pgdata2:/data -v $PWD:/backup alpine \
tar xzf /backup/pgdata.tgz -C /data
For databases, prefer logical backups (pg_dump) or stop the DB first for consistency.
More on Volumes & Bind Mounts
What are the three ways to persist or share data with a container?BeginnerVolume vs bind mount: when do you pick which?BeginnerWhat is the difference between `-v` and `--mount`?BeginnerWhat is an anonymous volume and how do you find leftovers?IntermediateHow does a new named volume get populated?IntermediateWhere is a named volume physically stored and can you edit it directly?Intermediate