Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to add data to an existing named volume in Docker?

Tags:

docker

I was using Docker in the old way, with a volume container:

docker run -d --name jenkins-data jenkins:tag echo "data-only container for Jenkins" 

But now I changed to the new way by creating a named volume:

 docker volume create --name my-jenkins-volume  

I bound this new volume to a new Jenkins container. The only thing I've left is a folder in which I have the /var/jenkins_home of my previous jenkins container. (by using docker cp) Now I want to fill my new named volume with the content of that folder.

Can I just copy the content of that folder to /var/lib/jenkins/volume/my-jenkins-volume/_data?

like image 714
DenCowboy Avatar asked May 26 '16 18:05

DenCowboy


People also ask

Which is a valid docker command to add a data volume?

Docker automatically creates a directory for the volume on the host under the /var/lib/docker/volume/ path. You can now mount this volume on a container, ensuring data persistence and data sharing among multiple containers.

How do I copy data to a docker volume?

You can use the -v option of docker run to copy volume data between a data volume container and the host. For example, you might want to back up the data so that you can restore it to the same data volume container or to copy it to a different data volume container.


1 Answers

You can certainly copy data directly into /var/lib/docker/volumes/my-jenkins-volume/_data, but by doing this you are:

  • Relying on physical access to the docker host. This technique won't work if you're interacting with a remote docker api.

  • Relying on a particular aspect of the volume implementation would could change in the future, breaking any processes you have that rely on it.

I think you are better off relying on things you can accomplish using the docker api, via the command line client. The easiest solution is probably just to use a helper container, something like:

docker run -v my-jenkins-volume:/data --name helper busybox true docker cp . helper:/data docker rm helper 
like image 115
larsks Avatar answered Nov 02 '22 02:11

larsks