-
-
Save shbatm/4488633200fe454d8b936fe4f0065736 to your computer and use it in GitHub Desktop.
Rename a docker volume via create/copy/rm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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