Reputation: 641
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?
Upvotes: 42
Views: 71656
Reputation: 6747
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.
Here is an example on how to do that:
# 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 \
ubuntu bash -c "cd /from ; cp -av . /to"
Upvotes: 64
Reputation: 20176
On Linux it can be as easy as copying a directory. Docker keeps volumes in /var/lib/docker/volumes/<volume_name>
, so you can simply copy contents of the source volume into a directory with another name:
# -a to preserve all file attributes (permissions, ownership, etc)
sudo cp -a /var/lib/docker/volumes/source_volume /var/lib/docker/volumes/target_volume
Upvotes: 50
Reputation: 41
Try to clone the volume using the "Volumes Backup & Share" Docker extension.
See https://www.docker.com/blog/back-up-and-share-docker-volumes-with-this-extension/
Upvotes: 4
Reputation: 111
Should you want to copy volumes managed by docker-compose, you'll also need to copy the specific labels when creating the new volume.
Else docker-compose will throw something like Volume already exists but was not created by Docker Compose
.
Extending on the solution by MauriceNino, these lines worked for me:
# Supplement "proj1_vol1" and "proj2_vol2" for your real volume names
docker volume inspect proj1_vol1 # Look at labels of old volume
docker volume create \
--label com.docker.compose.project=proj2 \
--label com.docker.compose.version=2.2.1 \
--label com.docker.compose.volume=vol2 \
proj2_vol2
docker container run --rm -it \
-v proj1_vol1:/from \
-v proj2_vol2:/to \
alpine ash -c "cd /from ; cp -av . /to"
Btw, this also seems to be the only way to rename Docker volumes.
Upvotes: 11