Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker volume: rename or copy operation

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?

like image 355
Jimmix Avatar asked Nov 16 '25 08:11

Jimmix


1 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

docker run --name myname -d -v prova:/usr/share/nginx/html nginx:latest
docker exec myname touch /usr/share/nginx/html/added_file
docker stop myname

Verify the volume has nginx data + our file added_file

sudo ls /var/lib/docker/volumes/prova/_data

Output:

50x.html  added_file  index.html

Migrate the data to volume prova2

docker run --rm \
  -v prova:/original \
  -v prova2:/migration \
  ubuntu:latest \
  bash -c "cp -R /original/* /migration/"

Verify the new volume has the same data

sudo ls /var/lib/docker/volumes/prova2/_data

Output:

50x.html  added_file  index.html

Run a new container with the migrated volume:

docker run --name copyname -d -v prova2:/user/share/nginx/html nginx:latest

Verify the new container sees the migrated data at the original volume moint point:

docker exec copyname ls -al /user/share/nginx/html

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!