How to copy data from docker volume to host?
Asked Answered
B

5

32

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 ?

Bistre answered 15/2, 2016 at 9:55 Comment(0)
H
59

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
Hurling answered 15/2, 2016 at 13:35 Comment(9)
@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 ?Bistre
Close. The container id is part of the source or destination, like scp uses a host nameHurling
@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 !Bistre
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.Hurling
Why do we need a temporary container?Jillene
@Jillene Docker doesn't provide another interface to access data from a volumeHurling
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 ./Postal
CID=$(docker run -d -v hello:/hello busybox true) will this return a container ID, and what if I already having a container in running state and I do have container ID, can I use it? as CID?Bogle
@AhmadMujtaba yes, existing container ID or container name can be usedHurling
A
14

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
Autry answered 18/3, 2023 at 9:22 Comment(1)
This is the simplest one.Eduction
D
3

Here is a one liner:

docker run --rm -v <volume>:/src -v $(pwd)/<dir>:/dest alpine sh -c 'cp -R /src/* /dest/'

Replace <volume> by the volume name & <dir> by a directory on the host (from the current location pwd)

This will:

  • Run a tiny alpine container
  • Mount the volume on src
  • Mount the host's <dir> on dest
  • Recursively copy src to dest
  • Stop the container
  • Delete the container
Dalton answered 25/11, 2023 at 3:13 Comment(0)
S
1

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.

Stanwinn answered 30/10, 2022 at 15:35 Comment(1)
Simple and more performant than the options relying on mounting another container (if you have sudo access, of course)Blackguard
B
0

We can do this by simply using Docker Desktop app. Volumes -> <look for the volume you want to export> -> click on export option next to the container id under Actions

Bogle answered 26/6 at 4:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.