Skip to content

Instantly share code, notes, and snippets.

@qoh
Last active March 29, 2021 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qoh/e676a402ab641f87a8f703aae903cee4 to your computer and use it in GitHub Desktop.
Save qoh/e676a402ab641f87a8f703aae903cee4 to your computer and use it in GitHub Desktop.
Create a copy of a Docker volume
#!/bin/sh
src="$1"
dest="$2"
if test "$src" = ""
then
echo "Usage: $0 <source-volume> [new-volume]" >&2
exit 1
fi
if ! docker volume inspect -- "$src" >/dev/null 2>&1
then
echo "Error: Cannot inspect source volume: $src" >&2
echo "Verify that the volume exists and you can access the Docker daemon." >&2
exit 2
fi
if test "$dest" != "" && docker volume inspect -- "$dest" >/dev/null 2>&1
then
echo "Error: Destination volume already exists: $dest" >&2
exit 3
fi
if ! dest="$(docker volume create -- "$dest")"
then
echo "Failed to create destination volume" >&2
exit 4
fi
echo "Copying data from source volume to destination volume" >&2
if ! docker run --rm -it -v "$src:/from:ro" -v "$dest:/to:rw" alpine ash -c "cd /from; cp -av . /to"
then
echo "Failed to copy data"
exit 5
fi
printf "%s\n" "$dest"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment