Kery Hu
Kery Hu

Reputation: 5906

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 ?

Upvotes: 38

Views: 38978

Answers (5)

Ahmad Mujtaba
Ahmad Mujtaba

Reputation: 105

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

Upvotes: 0

Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46208

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

Upvotes: 4

Matt
Matt

Reputation: 74871

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

Upvotes: 64

user3431976
user3431976

Reputation: 191

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

Upvotes: 18

Eric PASCUAL
Eric PASCUAL

Reputation: 219

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.

Upvotes: 1

Related Questions