Skip to content

Instantly share code, notes, and snippets.

@shbatm
Forked from djbender/rename-docker-volume.sh
Last active July 27, 2025 15:33
Show Gist options
  • Select an option

  • Save shbatm/4488633200fe454d8b936fe4f0065736 to your computer and use it in GitHub Desktop.

Select an option

Save shbatm/4488633200fe454d8b936fe4f0065736 to your computer and use it in GitHub Desktop.
Rename a docker volume via create/copy/rm
#!/bin/bash
# Exit on error
set -e
# Check for exactly 2 arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 OLD_VOLUME NEW_VOLUME"
exit 1
fi
OLD_VOLUME="$1"
NEW_VOLUME="$2"
# Check if NEW_VOLUME already exists
if docker volume inspect "${NEW_VOLUME}" &> /dev/null; then
echo "Volume '${NEW_VOLUME}' already exists. Skipping creation."
else
echo "Creating volume '${NEW_VOLUME}'..."
docker volume create --name "${NEW_VOLUME}"
fi
# Copy data from old to new volume
docker run --rm -it \
-v "${OLD_VOLUME}:/from" \
-v "${NEW_VOLUME}:/to" \
alpine ash -c "cd /from && cp -av . /to"
# Remove old volume
docker volume rm "${OLD_VOLUME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment