Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy docker volumes

Tags:

docker

volumes

I want to update some container. For testing, I want to create a copy of the corresponding volume. Set up a new container for this new volume.

Is this as easy as doing cp -r volumeOld volumeNew?

Or do I have to pay attention to something?

like image 760
Ma Sch Avatar asked May 17 '21 10:05

Ma Sch


1 Answers

To clone docker volumes, you can transfer your files from one volume to another one. For that you have to manually create a new volume and then spin up a container to copy the contents.

Someone has already made a script for that, which you might use: https://github.com/gdiepen/docker-convenience-scripts/blob/master/docker_clone_volume.sh

If not, use the following commands (taken from the script):

# Supplement "old_volume" and "new_volume" for your real volume names

docker volume create --name new_volume

docker container run --rm -it \
           -v old_volume:/from \
           -v new_volume:/to \
           alpine ash -c "cd /from ; cp -av . /to"
like image 110
MauriceNino Avatar answered Oct 05 '22 21:10

MauriceNino