27

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 ?

emilaz
  • 1,722
  • 1
  • 15
  • 31
Kery Hu
  • 5,626
  • 11
  • 34
  • 51

3 Answers3

53

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
Matt
  • 68,711
  • 7
  • 155
  • 158
  • 1
    @Matt...yes,thanks,this question has resolved ! But ...Can i ask a deep question : How can i copy local data to docker volume **hello**, i try : `CID=$(docker run -d -v hello:/hello busybox true)` then : `docker cp $CID: ./data /hello` ,,it has en error:`Error response from daemon: lstat /mnt/sda1/var/lib/docker/aufs/mnt/583e187 ..no such file or directory` ..Can help me ? – Kery Hu Feb 16 '16 at 02:03
  • Close. The container id is part of the source or destination, like `scp` uses a host name – Matt Feb 16 '16 at 02:23
  • @Matt...Sorry,i'm a newer to Docker , do not understand `The container id is part of the source or destination, like scp uses a host name` , Can you detail it ? and how to implement ? thanks ! – Kery Hu Feb 16 '16 at 03:22
  • No worries.. check the answer again. I edited it to show copying the other direction at the time of that comment, my scp comment was referring to those updates. – Matt Feb 16 '16 at 03:34
  • Why do we need a temporary container? – FreeSoftwareServers Jul 22 '22 at 17:41
  • @FreeSoftwareServers Docker doesn't provide another interface to access data from a `volume` – Matt Jul 23 '22 at 02:13
  • Worked for me thanks. You can also use a named container if you don't want to bother with IDs `docker run --name temp-name -d -v hello:/hello busybox true`, `docker cp temp-name:/hello ./` – Maxime Oct 31 '22 at 10:16
7

You do not need to run any additional container you can perform this task with the existing container.

Copy files from container to local path

docker cp CONTAINER:/var/logs/ /tmp/app_logs

Copy a local file into container

docker cp ./some_file CONTAINER:/work
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
user3431976
  • 81
  • 1
  • 2
0

While upgrading the OS to a new version by side installing the new one and installing at the same time a SSD in addition to the old mech. HD, I've just transferred volumes from one system to another by a simple file copy at the host level. The system is Linux Mint, and it should work the same for any *nix based box. Sorry by I don't know for Windows.

While running the new system:

  • create the Docker volumes
  • mount the disk of the old install
  • locate there the volumes to be copied (under /var/lib/docker/volumes/) based on the name
  • copy (copy -a -r) their _data folder content to their counterpart
  • enjoy

All these manipulations must be done as root of course.

After starting the Docker Compose stack on the new system, I've found all the data (including a PostgreSQL DB) there, exactly as they were in the old system.

Eric PASCUAL
  • 109
  • 1
  • 8