Back up a named volume to an archive

Low risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Archives the contents of a named Docker volume into a compressed tar file in the current directory, using a throwaway container. The volume is only read.

dockervolumebackup

Parameters

Name of the Docker volume to back up.

Commands

Confirm the volume exists before backing it up

docker volume inspect <volume>

Archive the volume's contents into a compressed tar file in the current directory

docker run --rm -v <volume>:/data:ro -v "$PWD":/backup alpine tar czf /backup/<volume>-backup.tar.gz -C /data .

Verification

tar tzf <volume>-backup.tar.gz

The archive lists the files that were in the volume.

Undo

Not undoable

The volume was only read, so there is nothing to undo there. The backup created a tar file in the current directory; delete it manually if it is not wanted.

Pitfalls

  • For consistent backups of databases, stop or quiesce the container writing to the volume first; a live copy can capture a mid-write state.
  • The volume is mounted read-only (:ro), so the backup container cannot modify it.
  • The Windows variant assumes PowerShell; in cmd.exe replace the working-directory reference accordingly.

Related