How to copy docker volume to local?

Kery Hu picture Kery Hu · Feb 15, 2016 · Viewed 11.4k times · Source

I create a docker volume "hello", and it contains some data .

How can i copy it to local ?

First :

kerydeMacBook-Pro:~ hu$ docker volume create --name hello
hello

checking :

kerydeMacBook-Pro:~ hu$ docker volume ls
DRIVER              VOLUME NAME
local               hello

volume "hello" inspect

kerydeMacBook-Pro:~ hu$  docker volume inspect hello
[
    {
        "Name": "hello",
        "Driver": "local",
        "Mountpoint": "/mnt/sda1/var/lib/docker/volumes/hello/_data"
    }
]

How can i copy volume "hello" to local ?

I try :

kerydeMacBook-Pro:~ hu$  docker cp hello:/mnt/sda1/var/lib/docker/volumes/hello/_data /Users/hu/Desktop/12
Error response from daemon: no such id: hello

It not works as expect !

Who can help me ?

Answer

Matt picture Matt · Feb 15, 2016

To copy data from the volume to the host, use a temporary container that has the volume mounted.

CID=$(docker run -d -v hello:/hello busybox true)
docker cp $CID:/hello ./

To copy a directory from the host to volume

cd local_dir
docker cp . $CID:/hello/

Then clean up the temporary container.

docker rm $CID