6

First I create a volume:

docker volume create --name some-volume

Then I create a file touch ~/somefile.txt

Now I want to move ~/somefile.txt into the root of the volume some-volume. How do I do this?

Ole
  • 41,793
  • 59
  • 191
  • 359

1 Answers1

5

You can do this with a container:

docker run --rm -v `pwd`:/source -v some-volume:/target \
  busybox cp -av /source/somefile.txt /target/somefile.txt

I will also use tar and stdin to pipe files into a remote volume over the docker client/server connection:

tar -cv -C source-dir . | \
  docker run --rm -i -v some-volume:/target busybox tar -xC /target

An export is similar:

docker run --rm -v some-volume:/source busybox tar -cC /source . | \
  tar -xC target-dir
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Thanks for the great answer - If I have a `backup.tar.gz` archive can I use the tar trick to unarchive the files directly into the volume without having to unarchive the backup? – Ole Mar 23 '17 at 21:03
  • Sure can, if it's the same host, you can do something like `docker run --rm -v $(pwd):/source -v some-volume:/target busybox tar -xf /source/backup.tar.gz -C /target` – BMitch Mar 23 '17 at 21:06
  • Thanks - You rock!! – Ole Mar 24 '17 at 21:24
  • Is it a nono to just mv somefile /var/lib/docker/volumes/some-volume/_data/ ? – Leif Neland Dec 10 '18 at 12:52
  • @LeifNeland Modifying the backend directly assumes that: you are root, docker has not changed anything, and your named volume is configured with only the default options. If any of these assumptions change, directly modifying the backend will break, while using a utility container should continue to work since it accesses the volume the same way your other containers do. – BMitch Dec 10 '18 at 13:00
  • I tried mv'ing a 16GB database backup into a volume, and while it did it instantaneously, it was not visible inside :-( So now I'm doing a docker cp, and it takes ages, but at least it is visible from inside. – Leif Neland Dec 10 '18 at 13:28
  • `docker cp` is for container filesystems, not volumes. So if that's the only way it's visible, your DB probably isn't saved in the volume and will be lost when you remove the container. – BMitch Dec 10 '18 at 13:30
  • dear @BMitch so if I have created a volume and I want to copy some directories into that volume I could use the same way and please could you clarify it a little bit. Thanks in advance – kikicoder Feb 25 '21 at 00:22
  • @kikicoder as long as tar/cp aren't overwriting files you wanted to preserve, it should work. – BMitch Feb 25 '21 at 15:09
  • @BMitch dear my data existing in an drive and it is so big about 200 gig therefore I was not able to move them into the volume is there any way to able accessing to the data from the container my container has a python installed and I have tried to mount my data dir to a tmp dir and then changed the working dir of the container to that tmp dir therefore I was able to run a python script but my script needs some data unfortunetly I could not access them where I get an assertion error for the data pathnote that dir that I have mounted has the data dir – kikicoder Feb 26 '21 at 17:14
  • @kikicoder to ask a new question, use the "Ask Question" button at the top of the site. – BMitch Feb 26 '21 at 20:22