Search code examples
dockerboot2dockerdocker-volume

How to copy data from docker volume to host?


I have created a docker volume "hello" and it contains some data .

How can I copy the data to host ?

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 the data on the volume "hello" to host?

I tried :

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 does not work as expected!

Who can help me ?


Solution

  • Docker does not provide an interface to access volumes directly but you can use a container that has the volume mounted. This may need a temporary container to be created:

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

    Copy a directory from volume to host

    docker cp $CID:/hello ./
    

    To copy a directory from the host to volume

    cd local_dir
    docker cp . $CID:/hello/
    

    Then clean up (if using a temporary container).

    docker rm $CID