As per documentation Docker volumes are advertised this way:
Volumes are the preferred mechanism for persisting data generated by and used by Docker containers. While bind mounts are dependent on the directory structure and OS of the host machine, volumes are completely managed by Docker.
But if they are so good, why there are no operations to manage them like: copy, rename?
the command:
docker volume --help
gives only these options:
Usage: docker volume COMMAND
Manage volumes
Commands:
create Create a volume
inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused local volumes
rm Remove one or more volumes
Documentation also states no other commands, nor any workarounds for having the copy or rename functionality.
I would like to rename currently existing volume and create another (blank) in place of the originally named volume and populate it with the new data for test.
After doing my test I may want (or not) to remove the newly created volume and rename the other one to its previous (original) name to restore the volume setup as it was before.
I would like to not create a backup of the original volume that I want to rename. Renaming is good enough for me and much faster than creating the backup and restoring form it.
Editing the docker-compose file and changing the name of the volume there is something I would like to avoid as well.
Is there any workaround that can work for renaming of a volume?
Can low level manual management from the shell targeting the Docker Root Dir: /var/lib/docker
and volumes
sub-dir be a solution or that approach may lead to some docker demon data inconsistency?
2
Answers
Not really the answer but I’ll post this copy example because I couldn’t find any before and searching for it took me to this question.
Docker suggest
--volumes-from
for backup purposes here.For offline migration (stopped container) I don’t see the point in using
--volumes-from
. So I just used a middle container with both volumes mounted and a copy command.To finish off the migration a new container can use the new volume
Here’s a quick test
Prepare a volume
prova
Verify the volume has nginx data + our file
added_file
Output:
Migrate the data to volume
prova2
Verify the new volume has the same data
Output:
Run a new container with the migrated volume:
Verify the new container sees the migrated data at the original volume moint point:
For next searchers, I made a script that can do a copy of volume by @Lennonry example. Here it is https://github.com/KOYU-Tech/docker-volume-copy
Script itself for history: