Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 260
Kery Hu Avatar asked Feb 15 '16 09:02

Kery Hu


People also ask

How do I copy a file from docker to hosting machine?

Obtain the name or id of the Docker container. Issue the docker cp command and reference the container name or id. The first parameter of the docker copy command is the path to the file inside the container. The second parameter of the docker copy command is the location to save the file on the host.

How do I export data from a docker container?

To export a container, we use the docker export command. The documentation describes export as follows: docker export – Export a container's filesystem as a tar archive. As we can see, this is just a regular old Linux file system — the BusyBox file system created when running our image, to be precise.


1 Answers

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
like image 183
Matt Avatar answered Oct 24 '22 04:10

Matt